Skip to content

Commit 64be527

Browse files
Day-01 q001 solved
1 parent 66e1c4d commit 64be527

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Problem Statement:
3+
There are n people standing in a circle (n is even). Each person looks at the person standing exactly opposite to them. Given three distinct integers a, b, and c: Person a is looking at person b. Find the person that person c is looking at. If no valid circle can be formed, print -1.
4+
5+
Approach:
6+
If person a looks at b, then the total number of people in the circle is:
7+
n = 2 * |a - b| For person c, if c + n/2 ≤ n, answer = c + n/2, else, answer = c - n/2 If n is zero or any of a, b, or c exceeds n, the configuration is invalid.
8+
9+
Time Complexity:
10+
O(1)
11+
12+
Space Complexity:
13+
O(1)*/
14+
15+
#include <bits/stdc++.h>
16+
using namespace std;
17+
int main(){
18+
int t;
19+
cin>>t;
20+
while(t--){
21+
long a,b,c;
22+
cin>>a>>b>>c;
23+
long n=2*abs(a-b);
24+
long ans=0;
25+
if(n==0||n<a||n<b||n<c){
26+
cout<<-1<<endl;
27+
continue;
28+
}
29+
if(c+(n/2)<=n){
30+
ans=c+(n/2);
31+
}
32+
else{
33+
ans=c-(n/2);
34+
}
35+
cout<<ans<<endl;
36+
}
37+
return 0;
38+
}

0 commit comments

Comments
 (0)