File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-02/sol/Sudhanshu Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+ typedef long long ll;
3+ typedef long double ld;
4+ using namespace std ;
5+
6+ int main ()
7+ {
8+ ios::sync_with_stdio (false );
9+ cin.tie (NULL );
10+
11+ ll t;
12+ cin >> t;
13+ while (t--)
14+ {
15+ ll n;
16+ cin >> n;
17+ vector<ll> v (n);
18+
19+ for (ll i = 0 ; i < n; i++)
20+ cin >> v[i];
21+
22+ sort (v.begin (), v.end ());
23+
24+ ll low = v[0 ];
25+ ll high = v[n - 1 ];
26+
27+ ll cLow = 0 ;
28+ ll cHigh = 0 ;
29+
30+ for (auto i : v)
31+ {
32+ if (i != low)
33+ break ;
34+ cLow++;
35+ }
36+ reverse (v.begin (), v.end ());
37+
38+ for (auto i : v)
39+ {
40+ if (i != high)
41+ break ;
42+ cHigh++;
43+ }
44+
45+ cout << ((low == high) ? (cLow * (cLow - 1 )) : (cLow * cHigh * 2 )) << endl;
46+ }
47+
48+ return 0 ;
49+ }
50+
51+ /*
52+ APPROACH:
53+
54+ - Sort the array to easily identify minimum and maximum elements
55+ - Find the minimum (low) and maximum (high) values
56+ - Count occurrences of minimum value (cLow) and maximum value (cHigh)
57+ - If all elements are equal (low == high), we need to choose any 2 elements from n,
58+ so answer = n * (n-1) (dividing by 2 would give combinations, but we want pairs)
59+ - Otherwise, the answer is cLow * cHigh * 2 (considering both orderings of pairs)
60+
61+ TC: O(nlog n)
62+ SC: O(n)
63+
64+ https://codeforces.com/contest/1771/submission/355619015
65+ */
You can’t perform that action at this time.
0 commit comments