File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-02/sol/Apoorv012 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments