Skip to content

Commit b91b85c

Browse files
authored
Create day2sol1.cpp
1 parent 66e1c4d commit b91b85c

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// submission link - https://codeforces.com/problemset/submission/1771/355587277
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
/*
7+
Approach:
8+
1. The maximum absolute difference between any two elements in the array
9+
is always (maximum element - minimum element).
10+
2. A pair (ai, aj) is interesting only if one of them is the minimum value
11+
and the other is the maximum value.
12+
3. Count how many times the minimum value appears (cntMin)
13+
and how many times the maximum value appears (cntMax).
14+
4. Since (ai, aj) and (aj, ai) are considered different pairs,
15+
the total number of interesting pairs is:
16+
2 * cntMin * cntMax
17+
5. Special case:
18+
- If all elements are equal (min == max), then every ordered pair
19+
(i, j) with i ≠ j is valid.
20+
- The answer becomes n * (n - 1).
21+
*/
22+
23+
int main(){
24+
25+
int t;
26+
cin >> t;
27+
while(t--){
28+
int n;
29+
cin >> n;
30+
31+
vector<long long> a(n);
32+
for(int i = 0; i < n; i++) cin >> a[i];
33+
34+
long long mn = *min_element(a.begin(), a.end());
35+
long long mx = *max_element(a.begin(), a.end());
36+
37+
if(mn == mx){
38+
cout << 1LL * n * (n - 1) << "\n";
39+
continue;
40+
}
41+
42+
long long cntMin = 0, cntMax = 0;
43+
for(long long x : a){
44+
if(x == mn) cntMin++;
45+
if(x == mx) cntMax++;
46+
}
47+
48+
cout << 2LL * cntMin * cntMax << "\n";
49+
}
50+
return 0;
51+
}

0 commit comments

Comments
 (0)