Skip to content

Commit 3116c5b

Browse files
authored
Merge pull request #338 from sahsudhanshu/Sol1Day2
Sol1 day2
2 parents c632414 + 9ab72a0 commit 3116c5b

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
*/

0 commit comments

Comments
 (0)