Skip to content

Commit 2e4d860

Browse files
committed
day 2 p1
1 parent 718754d commit 2e4d860

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// link : https://codeforces.com/problemset/submission/1771/355643992
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
#define fastio ios::sync_with_stdio(false); cin.tie(NULL);
7+
8+
using ll = long long;
9+
10+
void solve() {
11+
ll n;
12+
cin >> n;
13+
14+
vector<ll> a(n);
15+
for (ll i = 0; i < n; i++) {
16+
cin >> a[i];
17+
}
18+
19+
sort(a.begin(), a.end());
20+
21+
ll i = 1;
22+
while (i < n && a[i] == a[i - 1]) i++;
23+
24+
if (i == n) {
25+
cout << n * (n - 1) << "\n";
26+
return;
27+
}
28+
29+
ll j = 1;
30+
while (j < n && a[n - j] == a[n - j - 1]) j++;
31+
32+
cout << 2LL * i * j << "\n";
33+
}
34+
35+
int main() {
36+
fastio
37+
int t;
38+
cin >> t;
39+
while (t--) solve();
40+
}
41+
42+
43+
/*
44+
The maximum possible absolute difference in an array is obtained only by
45+
choosing the minimum and the maximum elements.
46+
47+
So, all valid pairs must consist of:
48+
one minimum element
49+
one maximum element
50+
51+
Approach:
52+
53+
1. Sort the array.
54+
Minimum elements appear at the beginning.
55+
Maximum elements appear at the end.
56+
57+
2. Count how many times the minimum element appears.
58+
Let this count be 'i'.
59+
60+
3. Count how many times the maximum element appears.
61+
Let this count be 'j'.
62+
63+
64+
65+
Time Complexity: O(n log n)
66+
Space Complexity: O(n)
67+
68+
*/

0 commit comments

Comments
 (0)