Skip to content

Commit 09d1cbf

Browse files
authored
Merge branch 'main' into main
2 parents 8c199e9 + dc3467a commit 09d1cbf

6 files changed

Lines changed: 246 additions & 1 deletion

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
using ll = long long;
5+
const ll MOD = 998244353;
6+
7+
void solve() {
8+
int a, b, c;
9+
cin >> a >> b >> c;
10+
11+
int diff = abs(a-b);
12+
if (a > 2*diff || b > 2*diff || c > 2*diff) {
13+
cout << -1 << endl;
14+
return;
15+
}
16+
17+
int ans = c + diff;
18+
if (ans > 2*diff) {
19+
ans -= 2*diff;
20+
}
21+
22+
cout << ans << endl;
23+
24+
}
25+
26+
int main() {
27+
ios::sync_with_stdio(false);
28+
cin.tie(nullptr);
29+
30+
int t = 1;
31+
cin >> t;
32+
33+
while (t--) {
34+
solve();
35+
}
36+
37+
return 0;
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Submission link: https://codeforces.com/contest/1560/submission/355246381
2+
3+
Time Complexity : O(1);
4+
5+
*/
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
int main(){
10+
int t;
11+
cin>>t;
12+
while(t--){
13+
long long int a,b,c;
14+
cin>>a>>b>>c;
15+
long long int maxval=2* abs(a-b);
16+
if(maxval>=a&&maxval>=b&&maxval>=c){
17+
long long int temp=2*abs(a-b);
18+
long long int ans=abs(a-b)+c;
19+
if(ans%temp==0){
20+
cout<<temp<<endl;
21+
}
22+
else{
23+
cout<<ans%temp<<endl;
24+
}
25+
}
26+
else{
27+
cout<<"-1\n";
28+
}
29+
}
30+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
#include<bits/stdc++.h>
42+
using namespace std;
43+
int main(){
44+
int t;
45+
cin>>t;
46+
while(t--){
47+
int n;
48+
cin>>n;
49+
long long a[n];
50+
for(int i=0;i<n;i++){
51+
cin>>a[i];
52+
}
53+
sort(a,a+n);
54+
if (a[0] == a[n - 1]) {
55+
cout << 1LL*n*(n-1)<< "\n";
56+
continue;
57+
}
58+
long long maxdiff=a[n-1]-a[0];
59+
long long min=0;
60+
long long max=0;
61+
for(int i=0;i<n;i++){
62+
if(a[i]==a[0]){
63+
min++;
64+
}
65+
if(a[i]==a[n-1]){
66+
max++;
67+
}
68+
69+
}
70+
cout<<2LL*max*min<<endl;
71+
}
72+
}
73+
//Time Complexity=O(t*nlogn+t*n)
74+
//space complexity=O(n)
23.8 KB
Binary file not shown.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Problem Statement:
3+
Hossam and Hazem want to count the number of interesting pairs (i, j) such that:
4+
1. 1 <= i, j <= n
5+
2. i != j
6+
3. |a[i] - a[j]| == max(|a[p] - a[q]|) for all 1 <= p, q <= n.
7+
8+
---------------------------------------
9+
Approach:
10+
1. Find the maximum and minimum values in the array. Let them be maxi and mini.
11+
2. The maximum absolute difference is diff = maxi - mini.
12+
3. We need to find pairs (a[i], a[j]) such that |a[i] - a[j]| = diff.
13+
4. Case 1: If maxi == mini (all elements are same), then any pair (i, j) with i != j works.
14+
The count is n * (n - 1).
15+
5. Case 2: If maxi != mini, we need one element to be maxi and the other to be mini.
16+
Count the occurrences of maxi (cntmax) and mini (cntmin).
17+
The number of pairs is 2 * cntmin * cntmax (since order matters.
18+
19+
----------------------------------------
20+
Time Complexity: O(n) - find min, max and their counts.
21+
Space Complexity: O(n) - store the input array.
22+
23+
----------------------------------------
24+
Example:
25+
Input:
26+
5
27+
6 2 3 8 1
28+
29+
Process:
30+
1. mini = 1, maxi = 8.
31+
2. diff = 7.
32+
3. cntmin (1) = 1.
33+
4. cntmax (8) = 1.
34+
5. Result = 2 * 1 * 1 = 2.
35+
36+
Output:
37+
2
38+
*/
39+
40+
#include <bits/stdc++.h>
41+
using namespace std;
42+
#define ll long long
43+
#define vll vector<ll>
44+
#define out(x) cout << (x) << endl
45+
#define outr(x) \
46+
{ \
47+
cout << x << endl; \
48+
return; \
49+
}
50+
51+
void solve()
52+
{
53+
ll n;
54+
cin >> n;
55+
vll a(n);
56+
int mini = INT_MAX;
57+
int maxi = INT_MIN;
58+
59+
for (int i = 0; i < n; i++)
60+
{
61+
cin >> a[i];
62+
if (a[i] < mini)
63+
mini = a[i];
64+
if (a[i] > maxi)
65+
maxi = a[i];
66+
}
67+
68+
if (mini == maxi)
69+
{
70+
cout << n * (n - 1) << endl;
71+
return;
72+
}
73+
74+
ll cntmin = 0;
75+
ll cntmax = 0;
76+
for (int x : a)
77+
{
78+
if (x == mini)
79+
cntmin++;
80+
if (x == maxi)
81+
cntmax++;
82+
}
83+
84+
cout << 2*cntmin*cntmax << endl;
85+
}
86+
87+
int main()
88+
{
89+
ios_base::sync_with_stdio(false);
90+
cin.tie(NULL);
91+
int t;
92+
cin >> t;
93+
while (t--)
94+
{
95+
solve();
96+
}
97+
return 0;
98+
}
99+
100+
//submission link: https://codeforces.com/problemset/submission/1771/355269691

contributers.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
|Priyanshu Ranjan | justpriyanshu | BIT Mesra
5454
| Vrajkumar Shah | vraj826 | DDU, Nadiad
5555
| Atharva Mehta | Atharva-insane | IIIT Allahabad |
56-
| Kushagra Raghuvanshi | Taskmaster-afk | IIIT Allahabad |
56+
| Kushagra Raghuvanshi | Taskmaster-afk | IIIT Allahabad | |
57+
| Apoorv Mittal | Apoorv012 | JIIT Noida |
58+
|Aman Kumar Mehta |AmanMehta22 |Amity University Jharkhand | |
59+
| Naman Pal | Naman2251 | IIIT Allahabad
5760
<!-- ADD ABOVE THIS -->
5861
<!-- example | Korvac | Betty | Reyansh College | -->

0 commit comments

Comments
 (0)