File tree Expand file tree Collapse file tree
Problems/Binary-Search/Day-06/sol/Naman2251 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Submission Link: https://codeforces.com/contest/2014/submission/356013624
2+ /*
3+ It is only not possible to call robinhood if n is 2 or 1.
4+ avg=(sum+k)/n.
5+ Since we need strictly more than half of the total population strictly less than half of the average wealth
6+ therefore we would just make our avg equal to a[n/2]+1 as then more than hal would be unhappy.
7+ (sum+k)/(n*2)=a[n/2]+1
8+ On solving, k=a[n/2]*n*2+1-sum
9+ If k<0 then no need of any coin so k=0, therefore final ans is max(k, 0).
10+ */
11+
12+ #include < bits/stdc++.h>
13+ using namespace std ;
14+
15+ int main () {
16+ long long t;
17+ cin>>t;
18+ while (t--) {
19+ long long n, sum=0 ;
20+ cin>>n;
21+ vector <long long > a (n);
22+ for (int i=0 ; i<n; i++) {
23+ cin>>a[i];
24+ sum+=a[i];
25+ }
26+ if (n<=2 ) {
27+ cout<<-1 <<endl;
28+ continue ;
29+ }
30+ sort (a.begin (), a.end ());
31+ long long k= a[n/2 ]*n*2 +1 -sum;
32+ cout<<max (k, 0LL )<<endl;
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments