|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#define endl '\n' |
| 3 | +typedef long long ll; |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +/* |
| 8 | +PROBLEM STATEMENT: |
| 9 | +There are n people living in the town. Just now, the wealth of the i-th person was ai |
| 10 | +
|
| 11 | +gold. But guess what? The richest person has found an extra pot of gold! |
| 12 | +
|
| 13 | +More formally, find an aj=max(a1,a2,…,an) |
| 14 | +, change aj to aj+x, where x |
| 15 | +
|
| 16 | +is a non-negative integer number of gold found in the pot. If there are multiple maxima, it can be any one of them. |
| 17 | +
|
| 18 | +A person is unhappy if their wealth is strictly less than half of the average wealth∗ |
| 19 | +
|
| 20 | +. |
| 21 | +
|
| 22 | +If strictly more than half of the total population n |
| 23 | +
|
| 24 | +are unhappy, Robin Hood will appear by popular demand. |
| 25 | +
|
| 26 | +Determine the minimum value of x |
| 27 | +for Robin Hood to appear, or output −1 if it is impossible. |
| 28 | +
|
| 29 | +
|
| 30 | +APPROACH: |
| 31 | +just following the conditions of the ques after sorting the array we get |
| 32 | +that x>2*n*a[mid]- sum where mid=n/2 and sum is sum of elements of array; |
| 33 | +the reason to sort them was that if the largest element satisfied the condition then all the subsequent |
| 34 | +smaller will do so automatically; |
| 35 | +and if n==1||n==2 ans would -1 reason simple most of them will be richer |
| 36 | +and if x is less than zero ans zero. |
| 37 | +
|
| 38 | +
|
| 39 | +TIME COMPLEXITY:O(n +nlogn(for sorting))so O(nlogn); |
| 40 | +SPACE COMPLEXITY:O(n); |
| 41 | +
|
| 42 | +
|
| 43 | +
|
| 44 | +SUBMISSION LINK:https://codeforces.com/contest/2014/submission/356057548 |
| 45 | +
|
| 46 | +
|
| 47 | +*/ |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +ll solve(){ |
| 54 | + int n;cin>>n;int a[n];ll sum=0; |
| 55 | + for(int i=0;i<n;i++){ |
| 56 | + cin>>a[i];sum+=a[i]; |
| 57 | + } |
| 58 | + if(n==1||n==2)return -1; |
| 59 | + else{ |
| 60 | + sort(a,a+n); |
| 61 | + int mid=n/2; |
| 62 | + ll ans=(ll)2*n*a[mid]-sum+1; |
| 63 | + if(ans>0)return ans; |
| 64 | + else return 0; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +int main(){ |
| 69 | + int t;cin>>t; |
| 70 | + while(t--){ |
| 71 | + cout<<solve()<<endl; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +} |
0 commit comments