Skip to content

Commit 853eecf

Browse files
solved first question of day2
1 parent 4ac2870 commit 853eecf

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <bits/stdc++.h>
2+
3+
#include <ext/pb_ds/assoc_container.hpp>
4+
#include <ext/pb_ds/tree_policy.hpp>
5+
6+
using namespace std;
7+
using namespace __gnu_pbds;
8+
9+
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
10+
// less makes it sorted greater makes it reverse sorted less equal makes it like multiset
11+
// we can also change the data type of the pbds
12+
13+
// Fast I/O
14+
#define FAST_IO \
15+
ios::sync_with_stdio(false); \
16+
cin.tie(NULL);
17+
#define pb push_back
18+
#define in insert
19+
#define int long long
20+
#define all(x) (x).begin(), (x).end()
21+
22+
// Typedefs for convenience
23+
typedef vector<int> vi;
24+
typedef pair<int, int> pii;
25+
26+
// Constants
27+
const int INF = 1e18;
28+
const int MOD = 1e9 + 7;
29+
30+
// Debug (can be disabled in contests)
31+
#ifdef DEBUG
32+
#define dbg(x) cerr << #x << " = " << x << '\n';
33+
#else
34+
#define dbg(x)
35+
#endif
36+
37+
// gcd
38+
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
39+
40+
// lcm
41+
int lcm(int a, int b)
42+
{
43+
if (a == 0 || b == 0)
44+
return 0;
45+
return abs(a * b) / gcd(a, b);
46+
}
47+
48+
// Binary exponentiation (modular)
49+
long long binexp(long long a, long long b, long long m)
50+
{
51+
a %= m;
52+
long long res = 1;
53+
while (b > 0)
54+
{
55+
if (b & 1)
56+
res = res * a % m;
57+
a = a * a % m;
58+
b >>= 1;
59+
}
60+
return res;
61+
}
62+
63+
void solve()
64+
{
65+
int n;
66+
cin >> n;
67+
vi v(n);
68+
for (int i = 0; i < n; i++)
69+
{
70+
cin >> v[i];
71+
}
72+
int mx = *max_element(all(v)), mi = *min_element(all(v));
73+
int cnt = 0, cnt2 = 0;
74+
for (int i = 0; i < n; i++)
75+
{
76+
if (v[i] == mx)
77+
cnt++;
78+
if (v[i] == mi)
79+
cnt2++;
80+
}
81+
if (mi == mx)
82+
{
83+
cout << (cnt * (cnt - 1)) << endl;
84+
}
85+
else
86+
{
87+
cout << 2 * (cnt * cnt2) << endl;
88+
}
89+
}
90+
91+
signed main()
92+
{
93+
FAST_IO
94+
int t = 1;
95+
cin >> t;
96+
while (t--)
97+
solve();
98+
return 0;
99+
}
100+
/*
101+
so basically we're asked in this question to compute the pairs whose diff is max(abs(ap-aq)) where p and q varies over all n
102+
now a simple thought would tell us that we need to count number of pairs such that one is max and other is min since max diff
103+
is only occured when substracting max - min
104+
105+
time complexity is o(n);
106+
space complexity is o(1);
107+
108+
submission id https://codeforces.com/contest/1771/submission/355258380
109+
110+
*/

0 commit comments

Comments
 (0)