Skip to content

Commit ae82bb3

Browse files
Merge pull request #157 from Tabassumasra05/main
created soln.cpp with link
2 parents 0ee062f + 638817c commit ae82bb3

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Problem:
3+
Given an even number of people standing in a circle, numbered clockwise from 1,
4+
each person is looking at the person directly opposite.
5+
Given numbers a,b,c,find the number being looked at by c,
6+
if a is looking at b.Output -1 if no such circle exists.
7+
8+
Approach :
9+
1. Let n be the total number of people in the circle.Since opposite person is at n/2 distance
10+
n=2*|a-b|
11+
2. If a,b,c are greater than n,output -1.
12+
3. The person opposite c is at position c + n/2.If this exceeds n,subtract n.
13+
14+
Time Complexity: O(1) per test case
15+
Space Complexity: O(1)
16+
17+
https://github.com/Tabassumasra05/CP-Chronicles/blob/main/Problems/Mathematics/Day-01/sol/solution1.cpp
18+
19+
*/
20+
21+
22+
#include<bits/stdc++.h>
23+
using namespace std;
24+
25+
void solve(){
26+
long long a,b,c,d;
27+
cin>>a>>b>>c;
28+
long long n=2*abs(a-b);
29+
if(a>n||b>n||c>n){
30+
cout<<-1<<endl;
31+
return;
32+
}
33+
d=c+(n/2);
34+
if(d>n){
35+
d-=n;
36+
}
37+
cout<<d<<endl;
38+
return;
39+
40+
41+
}
42+
43+
int main(){
44+
int t;
45+
cin>>t;
46+
while(t--){
47+
solve();
48+
}
49+
}

0 commit comments

Comments
 (0)