Skip to content

Commit ebfdd46

Browse files
authored
Added solution for prob 1
1 parent 4034e99 commit ebfdd46

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define int long long
5+
6+
/*
7+
PROBLEM
8+
- We have n people sitting circularly where n is even
9+
- We are given a, b, c where a is opposite to b
10+
- Find who is sitting opposite to c
11+
*/
12+
13+
/*
14+
15+
APPROACH
16+
17+
- As a is opposite to b, between a and b we will have 2*(a-b-1)+2 = 2abs(a-b)
18+
This determines the size of the circle (sz)
19+
20+
- Now we verify if the given a, b, c are valid or not which can be done by checking a,b,c <= sz
21+
22+
- If valid, the neighborr of c will be (c + sz/2) % SIZE OF CIRCLE (convert to 0 based to prevent a % a == 0)
23+
24+
*/
25+
26+
/*
27+
TIME COMPLEXITY - O(1)
28+
SPACE COMPLEXITY - O(1)
29+
*/
30+
31+
void solve() {
32+
int a, b, c;
33+
cin >> a >> b >> c;
34+
int sz = 2*abs(a-b);
35+
if (a <= sz && b <= sz && c <= sz) {
36+
int d = (c + sz/2 - 1) % sz + 1;
37+
cout << d << '\n';
38+
}
39+
else {
40+
cout << -1 << '\n';
41+
}
42+
}
43+
44+
45+
signed main() {
46+
int t;
47+
cin >> t;
48+
while (t--) {
49+
solve();
50+
}
51+
return 0;
52+
}

0 commit comments

Comments
 (0)