File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-01/sol/RonakGoyal Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ Problem Statement:
3+ Some number of people (this number is even) are standing evenly in a circle.
4+ Each person looks directly at the person opposite to them.
5+ Given that person a is looking at person b, determine whom person c
6+ is looking at. If no such circle configuration exists, output -1.
7+
8+ Approach:
9+ 1. The distance between opposites (diff) is half the total population.
10+ N = 2 * |a - b|
11+ 2. Validity Check: a, b, and c must all be less than or equal to N.
12+ 3. To find the opposite of c:
13+ - If c <= N/2, its opposite is c + N/2.
14+ - If c > N/2, its opposite is c - N/2.
15+
16+ Time Complexity:
17+ O(1) per test case
18+
19+ Space Complexity:
20+ O(1)
21+ Example:
22+ 7
23+ 6 2 4
24+ 2 3 1
25+ 2 4 10
26+ 5 3 4
27+ 1 3 2
28+ 2 5 4
29+ 4 3 2
30+ Output:
31+ 8
32+ -1
33+ -1
34+ -1
35+ 4
36+ 1
37+ -1
38+
39+ Submission Link : https://codeforces.com/contest/1560/submission/355273828
40+ */
41+
42+ #include < bits/stdc++.h>
43+ using namespace std ;
44+
45+ int main () {
46+ int t;
47+ cin >> t;
48+
49+ while (t--) {
50+ long long a, b, c;
51+ cin >> a >> b >> c;
52+
53+ long long diff = llabs (a - b);
54+ long long N = 2 * diff;
55+
56+ if (a > N || b > N || c > N) {
57+ cout << -1 << endl;
58+ } else {
59+ long long half = N / 2 ;
60+ long long d;
61+ if (c <= half) {
62+ d = c + half;
63+ } else {
64+ d = c - half;
65+ }
66+ cout << d << endl;
67+ }
68+ }
69+
70+ return 0 ;
71+ }
72+
73+
You can’t perform that action at this time.
0 commit comments