Skip to content

Commit cc66e5e

Browse files
authored
Add Solution1.cpp for Day-01 Mathematics problem
1 parent 98005d0 commit cc66e5e

1 file changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define int long long
5+
#define ll long long
6+
#define ull unsigned long long
7+
#define ld long double
8+
#define pii pair<int,int>
9+
#define vi vector<int>
10+
#define vvi vector<vector<int>>
11+
#define vpi vector<pair<int,int>>
12+
#define all(x) (x).begin(), (x).end()
13+
14+
15+
const long long MAXN = 2e6;
16+
const long long MOD = 1e9 + 7;
17+
const int INF = 1e18;
18+
const ld EPS = 1e-9;
19+
20+
21+
#define fastio ios::sync_with_stdio(false); cin.tie(nullptr);
22+
23+
#ifndef ONLINE_JUDGE
24+
#define debug(x) cerr << #x << " = " << x << "\n";
25+
#else
26+
#define debug(x)
27+
#endif
28+
29+
// Utility functions
30+
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
31+
int lcm(int a, int b) { return a / gcd(a, b) * b; }
32+
int mod_add(int a, int b, int m=MOD) { return ((a % m) + (b % m) + m) % m; }
33+
int mod_sub(int a, int b, int m=MOD) { return ((a % m) - (b % m) + m) % m; }
34+
int mod_mul(int a, int b, int m=MOD) { return ((a % m) * (b % m)) % m; }
35+
36+
//Binary Exponentiation
37+
int binexp(int a, int b, int m=MOD) {
38+
int res = 1;
39+
a %= m;
40+
while(b > 0) {
41+
if(b & 1) res = (res * a) % m;
42+
a = (a * a) % m;
43+
b >>= 1;
44+
}
45+
return res;
46+
}
47+
48+
int mod_inv(int a, int m=MOD) {
49+
return binexp(a, m - 2, m);
50+
}
51+
52+
long long fac[MAXN + 1];
53+
long long inv[MAXN + 1];
54+
55+
long long exp(long long x, long long n, long long m) {
56+
x %= m;
57+
long long res = 1;
58+
while (n > 0) {
59+
if (n % 2 == 1) { res = res * x % m; }
60+
x = x * x % m;
61+
n /= 2;
62+
}
63+
return res;
64+
}
65+
66+
void factorial() {
67+
fac[0] = 1;
68+
for (long long i = 1; i <= MAXN; i++) { fac[i] = fac[i - 1] * i % MOD; }
69+
}
70+
71+
void inverses() {
72+
inv[MAXN] = exp(fac[MAXN], MOD - 2, MOD);
73+
for (long long i = MAXN; i >= 1; i--) { inv[i - 1] = inv[i] * i % MOD; }
74+
}
75+
76+
long long choose(long long n, long long r) {
77+
if (r > n)return 0ll;
78+
return (fac[n] * inv[r] % MOD * inv[n - r] % MOD) % MOD;
79+
}
80+
81+
long long catalan(long long n) {
82+
return (exp(n + 1, MOD - 2, MOD) % MOD * choose(2 * n, n) % MOD) % MOD;
83+
}
84+
85+
//Returns all prime numbers <=N
86+
vector<int> sieve(int n) {
87+
vector<bool> prime(n + 1, true);
88+
for (int p=2;p*p<=n;p++) {
89+
if (prime[p] == true) {
90+
91+
for (int i=p*p;i<=n;i+=p)
92+
prime[i] = false;
93+
}
94+
}
95+
96+
vector<int> res;
97+
for (int p = 2; p <= n; p++){
98+
if (prime[p]){
99+
res.push_back(p);
100+
}
101+
}
102+
return res;
103+
}
104+
105+
//Prime factors of all numbers upto N
106+
vector<vector<int>> primefactors(int N)
107+
{
108+
vvi pfac(N + 1);
109+
for (int i=2;i<=N;i++){
110+
if (!pfac[i].empty())
111+
continue;
112+
113+
for (int j = i; j <= N; j += i)
114+
pfac[j].push_back(i);
115+
}
116+
117+
return pfac;
118+
}
119+
120+
//smallest prime factor of a number
121+
int spf(int n) {
122+
if (n % 2 == 0) return 2;
123+
for (int i = 3; i * i <= n; i += 2) {
124+
if (n % i == 0) return i;
125+
}
126+
return n;
127+
}
128+
129+
int power2(int p)
130+
{
131+
int v=1ll<<p;
132+
return v;
133+
}
134+
135+
//Sort functions
136+
137+
void sort(vector<int>&a)
138+
{
139+
sort(a.begin(),a.end());
140+
}
141+
142+
void psort(vector<pair<int,int>>&a)
143+
{
144+
sort(a.begin(),a.end());
145+
}
146+
147+
void rsort(vector<int>&a)
148+
{
149+
sort(a.rbegin(),a.rend());
150+
}
151+
152+
void rpsort(vector<pair<int,int>>&a)
153+
{
154+
sort(a.rbegin(),a.rend());
155+
}
156+
157+
//2-D Vector Declaration:
158+
//vvi mat(n,vi(m)); // nxm matrix, all initialized to 0
159+
160+
161+
/***************Code***************/
162+
163+
void solve() {
164+
165+
int a,b,c;
166+
cin>>a>>b>>c;
167+
168+
int diff=abs(a-b);
169+
int p=2*diff;
170+
if(a<=p&&b<=p&&c<=p)
171+
{
172+
if(c>diff)
173+
cout<<c-diff<<endl;
174+
else
175+
cout<<c+diff<<endl;
176+
}
177+
else
178+
cout<<"-1"<<endl;
179+
}
180+
181+
182+
signed main() {
183+
fastio;
184+
int t = 1;
185+
cin >> t;
186+
while (t--) {
187+
solve();
188+
}
189+
return 0;
190+
}

0 commit comments

Comments
 (0)