Skip to content

Commit 1a51d5b

Browse files
authored
Merge branch 'main' into main
2 parents 2cca60a + a1276cd commit 1a51d5b

12 files changed

Lines changed: 511 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
/*
4+
Logic:
5+
The main key in solving the problem is that a,b will be enought to know that the size of the area.
6+
Another observation would be that 1 is always facing the element more than the element which faces
7+
at the last element or the size of the circle.
8+
Once, you get the size of the array, we just have to check if the any of the elements are larger than
9+
the size of the array.
10+
On deeper checks like- a=3, b=2,c=1;
11+
we can observe how this isn't true but how does the conditon fulfill the case?
12+
--> the max check covers all small checks, like the circle don being possible
13+
in cases like a=1,b=6, c=100
14+
--> the circle condition is also checked by the max case.
15+
16+
So, after the checking, we need to see the answer, that can be easily given as the
17+
difference is same for all pairs of elements modular nature of the circle( cool words that make sense)
18+
19+
Time complexity-->
20+
21+
The time complexity is O(1)
22+
The space complexity is O(1)
23+
24+
Submission link- https://codeforces.com/contest/1560/submission/355188467
25+
*/
26+
//Soln->
27+
int main() {
28+
ios::sync_with_stdio(false);
29+
cin.tie(nullptr);
30+
31+
int t;
32+
cin >> t;
33+
34+
while (t--) {
35+
int a,b,c;
36+
cin>>a;
37+
cin>>b;
38+
cin>>c;
39+
int x=abs(b-a);
40+
if(max({a,b,c})>(2*x))
41+
cout<<-1<<endl;
42+
else
43+
{
44+
if(c>x)
45+
cout<<c-x<<endl;
46+
else
47+
cout<<c+x<<endl;
48+
}
49+
50+
}
51+
return 0;
52+
}
53+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://codeforces.com/problemset/submission/1560/355220642
2+
3+
#include <iostream>
4+
#include <cmath>
5+
using namespace std;
6+
int main()
7+
{
8+
int t;
9+
cin >> t;
10+
while (t--)
11+
{
12+
long long a, b, c;
13+
cin >> a >> b >> c;
14+
long long order = fabs(a - b);
15+
if ((2 * order < a) || (2 * order < b) || (2 * order < c))
16+
cout << -1 << endl;
17+
else if (order < c)
18+
cout << c - order << endl;
19+
else
20+
cout << c + order << endl;
21+
}
22+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*PROBLEM STATEMENT :
2+
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).
3+
4+
Each person is looking at the person standing exactly opposite him in the circle.
5+
6+
You are given three distinct integers a, b, and c. It is known that person a is looking at person b.
7+
8+
Determine the number of the person that person c is looking at.
9+
If there are multiple answers, print any of them.
10+
If there is no answer, print -1.
11+
12+
Approach : the length of the circle will be |a-b|*2 so if any of a,b,c is greater than this we print -1 if this is not true we print c+|a-b| if it exists in the circle otherwise we print c-|a-b|
13+
Time Complexity : O(1)
14+
Space Complexity : O(1)
15+
Input
16+
7
17+
6 2 4
18+
2 3 1
19+
2 4 10
20+
5 3 4
21+
1 3 2
22+
2 5 4
23+
4 3 2
24+
Output
25+
8
26+
-1
27+
-1
28+
-1
29+
4
30+
1
31+
-1
32+
https://codeforces.com/problemset/submission/1560/355227237
33+
*/
34+
#include <iostream>
35+
using namespace std;
36+
int main() {
37+
int t;
38+
cin >>t;
39+
while(t--)
40+
{
41+
int a,b,c;
42+
cin >>a >>b>>c;
43+
int len=abs(a-b)*2;
44+
if(c>len||a>len||b>len)
45+
cout <<-1<<"\n";
46+
else
47+
{
48+
if(c+len/2<=len)
49+
cout <<c+len/2<<"\n";
50+
else
51+
cout << c-len/2<<"\n";
52+
}
53+
54+
}
55+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
ios_base::sync_with_stdio(false);
6+
cin.tie(NULL);
7+
8+
int t;
9+
cin >> t;
10+
while (t--) {
11+
int a, b, c;
12+
cin >> a >> b >> c;
13+
int n = 2*(abs(a-b));
14+
15+
if(a == b){
16+
cout << -1 << endl;
17+
continue;
18+
}
19+
20+
if(a > n || b > n || c > n){
21+
cout << -1 << endl;
22+
continue;
23+
}
24+
25+
if(c + (n/2) <= n){
26+
cout << (c+(n/2)) << endl;
27+
}else{
28+
cout << (c-(n/2)) << endl;
29+
}
30+
31+
}
32+
return 0;
33+
}
34+
35+
36+
37+
38+
/* Explaination : The opposite numbers must be having exactly n/2 numbers in between, hence the total number, ie. n = 2*abs(a-b).
39+
40+
Time Complexity : O(t), t is the number of test cases.
41+
42+
Submission Link : https://codeforces.com/contest/1560/submission/355247479
43+
44+
*/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define umpp(i, j) unordered_map<i, j, custom_hash>
5+
#define mpp(i, j) map<i, j>
6+
#define int long long
7+
#define ll long long
8+
#define vi vector<int>
9+
#define pii pair<int, int>
10+
#define vpii vector<pair<int,int>>
11+
#define pb push_back
12+
#define pob pop_back
13+
#define sz(x) ((int)(x).size())
14+
#define all(v) v.begin(), v.end()
15+
#define F first
16+
#define S second
17+
#define sortall(x) sort(all(x))
18+
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
19+
#define forx(x, n) for (int x = 0; x < (n); x++)
20+
#define PI 3.1415926535897932384626
21+
const int mod = 1e9 + 7;
22+
23+
void solve() {
24+
int a,b,c; cin >> a>>b>>c;
25+
int diff = abs(a - b);
26+
int t = 2 * diff;
27+
if(a>t || b>t||c>t){
28+
cout << -1 << endl;
29+
}
30+
else{
31+
int a1 = c - diff;
32+
int a2 = c + diff;
33+
if(a1>=1 && a1<=t){
34+
cout << a1 << endl;
35+
return;
36+
}
37+
cout << a2 << endl;
38+
}
39+
}
40+
41+
signed main() {
42+
ios_base::sync_with_stdio(0);
43+
cin.tie(0);
44+
cout.tie(0);
45+
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
46+
int t = 1;
47+
cin >> t;
48+
while (t--) solve();
49+
return 0;
50+
}
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## Problem Description
2+
3+
Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.
4+
5+
You are given an array of `n` positive integers
6+
`a1, a2, ..., an`.
7+
8+
Hossam will choose a number `ai` and Hazem will choose a number `aj`.
9+
10+
Your task is to count the number of index pairs `(i, j)` such that:
11+
12+
- `1 ≤ i, j ≤ n`
13+
- `i ≠ j`
14+
- The absolute difference `|ai − aj|` is equal to the **maximum absolute difference** over all pairs in the array
15+
16+
---
17+
18+
## Input Format
19+
20+
- The first line contains a single integer `t` — the number of test cases.
21+
- For each test case:
22+
- The first line contains an integer `n`
23+
- The second line contains `n` integers `a1, a2, ..., an`
24+
25+
---
26+
27+
## Output Format
28+
29+
- For each test case, print a single integer — the number of valid pairs `(i, j)`.
30+
31+
---
32+
33+
## Constraints
34+
35+
- `1 ≤ t ≤ 100`
36+
- `2 ≤ n ≤ 100000`
37+
- `1 ≤ ai ≤ 100000`
38+
- The sum of `n` over all test cases does not exceed `100000`
39+
40+
---
41+
42+
43+
## Sample Input
44+
45+
```
46+
2
47+
5
48+
6 2 3 8 1
49+
6
50+
7 2 8 3 2 10
51+
```
52+
---
53+
54+
## Output
55+
56+
```
57+
2
58+
4
59+
```
60+
61+
[PROBLEM LINK](https://codeforces.com/problemset/problem/1771/A)
62+

0 commit comments

Comments
 (0)