Skip to content

Commit 2b61431

Browse files
committed
My soln. for q1
1 parent 2abd4b1 commit 2b61431

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: Solution1","url":"c:\\Users\\Harshvardhan\\Downloads\\CP-Chronicles\\Problems\\Mathematics\\Day-01\\sol\\HARSHVARDHAN\\Solution1.cpp","tests":[{"id":1766761298231,"input":"7\n6 2 4\n2 3 1\n2 4 10\n5 3 4\n1 3 2\n2 5 4\n4 3 2","output":"8\n-1\n-1\n-1\n4\n1\n-1"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\Harshvardhan\\Downloads\\CP-Chronicles\\Problems\\Mathematics\\Day-01\\sol\\HARSHVARDHAN\\Solution1.cpp","group":"local","local":true}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*PROBLEM STATEMENT :
2+
3+
There are n people standing in a circle. They are numbered from 1 to n in a clockwise order. The circle is even (i.e. n is even).
4+
5+
Each person is looking at the person standing exactly opposite him in the circle.
6+
7+
You are given three distinct integers a, b, and c. It is known that person a is looking at person b.
8+
9+
Determine the number of the person that person c is looking at.
10+
If there are multiple answers, print any of them.
11+
If there is no answer, print -1.
12+
13+
Approach : Total(i.e. n) = 2*(difference of a and b), so if any of the a,b,c is greater than total, it will return -1. Else we should return c + |a-b| or c-|a-b| (only one of them will be feasible)
14+
15+
Time Complexity: O(1) because we have created a formula directly.
16+
Space Complexity : O(1) because no. of inputs is fixed
17+
18+
Input
19+
7
20+
6 2 4
21+
2 3 1
22+
2 4 10
23+
5 3 4
24+
1 3 2
25+
2 5 4
26+
4 3 2
27+
Output
28+
8
29+
-1
30+
-1
31+
-1
32+
4
33+
1
34+
-1 */
35+
36+
#include<bits/stdc++.h>
37+
#include<array>
38+
#include<unordered_set>
39+
using namespace std;
40+
typedef long long ll;
41+
#define int long long int
42+
#define F first
43+
#define S second
44+
#define pb push_back
45+
#define si set <int>
46+
#define vi vector <int>
47+
#define pii pair <int, int>
48+
#define vpi vector <pii>
49+
#define vpp vector <pair<int, pii>>
50+
#define mii map <int, int>
51+
#define mpi map <pii, int>
52+
#define spi set <pii>
53+
#define endl "\n"
54+
#define sz(x) ((int) x.size())
55+
#define all(p) p.begin(), p.end()
56+
#define double long double
57+
#define que_max priority_queue <int>
58+
#define que_min priority_queue <int, vi, greater<int>>
59+
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
60+
#define vin(a) for(int i = 0; i< a.size();i++) cin>>a[i]
61+
#define vout(a) {for(auto x : a) cout << x << " "; cout << endl;}
62+
#define pp(a) for(auto x : a) cout << x.F << " " << x.S << endl
63+
#define print(a,x,y) {for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl}
64+
65+
inline int power(int a, int b){int x = 1;while (b){if (b & 1) x *= a;a *= a;b >>= 1;}return x;}
66+
template <typename Arg1>
67+
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
68+
template <typename Arg1, typename... Args>
69+
void __f (const char* names, Arg1&& arg1, Args&&... args)
70+
{
71+
const char* comma = strchr (names + 1, ',');
72+
cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
73+
}
74+
75+
bool isPrime(int n){ if(n <= 1) return false; for(int i=2;i*i<=n;i++) if(n % i == 0) return false; return true; }
76+
ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
77+
ll lcm(ll a, ll b){ return a / gcd(a,b) * b; }
78+
ll modpow(ll a, ll b, ll m){ ll r=1; while(b){ if(b&1) r=r*a%m; a=a*a%m; b>>=1;} return r; }
79+
80+
bool getBit(ll x, int k){ return (x >> k) & 1; }
81+
ll setBit(ll x, int k){ return x | (1LL << k); }
82+
ll clearBit(ll x, int k){ return x & ~(1LL << k); }
83+
ll toggleBit(ll x, int k){ return x ^ (1LL << k); }
84+
int countBits(ll x){ return __builtin_popcountll(x); }
85+
bool isPowerOfTwo(ll x){ return x && !(x & (x - 1)); }
86+
87+
vector<int> sieve(int n){ vector<int> p(n+1,1), primes; p[0]=p[1]=0; for(int i=2;i*i<=n;i++) if(p[i]) for(int j=i*i;j<=n;j+=i) p[j]=0; for(int i=2;i<=n;i++) if(p[i]) primes.pb(i); return primes; }
88+
89+
const int N = 200005;
90+
91+
void solve() {
92+
int a,b,c;
93+
cin >> a >> b >> c;
94+
95+
int total = 2*abs(a-b);
96+
if (max(max(a,b),c)>total)
97+
{
98+
cout << -1 << endl;return;
99+
}
100+
else
101+
{
102+
int ans = ((c-abs(a-b))>0 )? c-abs(a-b) : c+ abs(a-b) ;
103+
cout << ans << endl;return;
104+
}
105+
}
106+
107+
int32_t main()
108+
{
109+
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
110+
int t = 1;
111+
cin >> t;
112+
while (t--) solve();
113+
return 0;
114+
}

0 commit comments

Comments
 (0)