Skip to content

Commit 7d5f67b

Browse files
committed
Day 2 Solution 1
1 parent 97c6f46 commit 7d5f67b

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
3+
Count the number of interesting pairs (ai,aj) that have the max. absolute difference.
4+
5+
Ans = count(min el) * count(max el)
6+
7+
Time: O(n)
8+
Space: O(1)
9+
10+
Submission Link: https://codeforces.com/contest/1771/submission/355675072
11+
12+
*/
13+
14+
#include <bits/stdc++.h>
15+
using namespace std;
16+
17+
using ll = long long;
18+
19+
int main() {
20+
ll t;
21+
cin >> t;
22+
while (t--) {
23+
ll n;
24+
cin >> n;
25+
unordered_map<ll, ll> mp;
26+
ll _max = LONG_LONG_MIN, _min = LONG_LONG_MAX;
27+
for (ll i = 0; i < n; i++) {
28+
ll el;
29+
cin >> el;
30+
mp[el]++;
31+
_max = max(_max, el);
32+
_min = min(_min, el);
33+
}
34+
ll ans;
35+
if (_max == _min) {
36+
ans = n * (n-1);
37+
}
38+
else {
39+
ans = 2 * mp[_min] * mp[_max];
40+
}
41+
cout << ans << endl;
42+
}
43+
}

0 commit comments

Comments
 (0)