File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-01/sol/Saurav_Gitte Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ {
2+ "files.associations" : {
3+ "iostream" : " cpp"
4+ }
5+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Problem Statement:
3+ There are n people standing in a circle (n is even). Each person looks at the person
4+ standing exactly opposite them. Given that person a looks at person b, determine
5+ the person that person c is looking at.
6+
7+ ---------------------------------------
8+ Approach:
9+ If 'a' looks at 'b', then the distance between a and b must be exactly n/2.
10+ Let dist = |a - b|. Then n = 2 * dist.
11+ Once n is known, the person opposite to c is:
12+ - c + dist, if it does not exceed n
13+ - otherwise, c - dist
14+
15+ If c is greater than n, then the configuration is invalid.
16+
17+ ----------------------------------------
18+
19+ Time Complexity: O(1)
20+
21+ Space Complexity: O(1)
22+
23+ ----------------------------------------
24+ Example:
25+ Input:
26+ a = 1, b = 3, c = 2
27+
28+ Process :
29+ 1. Calculate dist = |1 - 3| = 2
30+ 2. Calculate n = 2 * dist = 4
31+ 3. Since c = 2, calculate opposite:
32+ - c + dist = 2 + 2 = 4 (which is <= n)
33+ - So, the person opposite to c is 4.
34+
35+ Output:
36+ 4
37+
38+ --------------------------------
39+ */
40+
41+ #include < bits/stdc++.h>
42+ using namespace std ;
43+
44+ int main () {
45+ int t;
46+ cin >> t;
47+ while (t--) {
48+ int a, b, c;
49+ cin >> a >> b >> c;
50+
51+
52+ int d = abs (a - b);
53+ int n = 2 * d;
54+ // 2*10^8 fits in int.
55+
56+ // Invalid configuration, Answer does not exist
57+ if (d == 0 || c > n) {
58+ cout << -1 << " \n " ;
59+ continue ;
60+ }
61+
62+ int ans;
63+ if (c + d <= n)
64+ ans = c + d;
65+ else
66+ ans = c - d;
67+
68+ cout << ans << " \n " ;
69+ }
70+ return 0 ;
71+ }
You can’t perform that action at this time.
0 commit comments