Skip to content

Commit f5ab7d7

Browse files
committed
Solved Issue2
1 parent 577087e commit f5ab7d7

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp"
4+
}
5+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

0 commit comments

Comments
 (0)