File tree Expand file tree Collapse file tree
Problems/Binary-Search/Day-06/sol/Aiyaan_Mahajan Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ DAY 6
3+ Q1 Robin hood in the town
4+ .*/
5+ /*
6+ We need more than half the people to have wealth
7+ < half of the new average.
8+
9+ Adding gold only increases the average, so poor people become “unhappy” first.
10+
11+ So we find the smallest
12+ 𝑥
13+ x such that the poorest majority are still below:
14+ half-average= (S + x)/2*n
15+
16+ Solve for
17+ x, and if it’s negative → answer is 0.
18+ If 𝑛≤2
19+ it’s impossible → -1.
20+
21+ That’s it.
22+
23+ */
24+
25+ #include < bits/stdc++.h>
26+ using namespace std ;
27+
28+ int main () {
29+
30+ int t;
31+ cin >> t;
32+ while (t--) {
33+ int n;
34+ cin >> n;
35+ vector<long long > a (n);
36+ long long S = 0 ;
37+ for (auto &x : a) {
38+ cin >> x;
39+ S += x;
40+ }
41+
42+ if (n <= 2 ) {
43+ cout << -1 << ' \n ' ;
44+ continue ;
45+ }
46+
47+ sort (a.begin (), a.end ());
48+ int m = n / 2 + 1 ;
49+ long long v = a[m - 1 ];
50+
51+ long long x = 2LL * n * v - S + 1 ;
52+ if (x < 0 ) x = 0 ;
53+
54+ cout << x << ' \n ' ;
55+ }
56+ return 0 ;
57+ }
58+
59+
60+
61+
62+
63+ // TC =O(Nlog(N)) and SC = O(N)
64+ /*
65+ My submission : https://codeforces.com/contest/2014/submission/356052582
66+ */
You can’t perform that action at this time.
0 commit comments