Skip to content

Commit 9cfa407

Browse files
authored
Merge branch 'opencodeiiita:main' into issue2
2 parents c954f2a + a44473e commit 9cfa407

22 files changed

Lines changed: 1756 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
*/
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//problem link- https://codeforces.com/contest/2014/problem/C
2+
//submission link- https://codeforces.com/contest/2014/submission/356102395
3+
//approach- (sum+x)/2*n needs to be greater than median for valid x
4+
// so I wrote binary search to find minimum x such that sum+x>median*2*n
5+
// as x will follow predicate function(FFFFTTTTTT)
6+
//Time complexity- O(nlogn)
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
#include <bits/stdc++.h>
17+
using namespace std;
18+
#define endl '\n'
19+
#define sp << " " <<
20+
#define int long long
21+
#define fora(a) for(auto i:a)
22+
#define gcd(a,b) __gcd(a,b)
23+
#define lcm(a,b) (a*(b/gcd(a,b)))
24+
#define forn(i,n) for(int i=0; i<n; i++)
25+
#define printv(a) {for(auto u:a) cout<<u<<" "; cout<<endl;}
26+
#define inputv(a) {for(int i=0;i<n;i++) cin>>a[i];}
27+
#define ll long long
28+
#define vi vector<int>
29+
#define vll vector<long long>
30+
#define pii pair<int, int>
31+
#define pll pair<long long, long long>
32+
#define all(x) x.begin(),x.end()
33+
34+
void fast_io() {
35+
ios_base::sync_with_stdio(false);
36+
cin.tie(NULL);
37+
cout.tie(NULL);
38+
}
39+
40+
41+
const int MOD = 1e9 + 7;
42+
ll addMod(ll a, ll b) { return (a + b) % MOD; }
43+
ll subMod(ll a, ll b) { return (a - b + MOD) % MOD; }
44+
ll mulMod(ll a, ll b) { return (a * b) % MOD; }
45+
46+
ll powerMod(ll a, ll b) {
47+
ll res = 1;
48+
while (b > 0) {
49+
if (b & 1) res = mulMod(res, a);
50+
a = mulMod(a, a);
51+
b >>= 1;
52+
}
53+
return res;
54+
}
55+
56+
57+
58+
59+
60+
void solve() {
61+
int n;
62+
cin>>n;
63+
vi a(n);
64+
inputv(a);
65+
sort(all(a));
66+
int sum=accumulate(all(a),0LL);
67+
if(n==1||n==2){cout<<-1<<endl;return;}
68+
int low=0;
69+
int high=2*n*a[n/2];
70+
while(high-low>=0){
71+
int mid=low+(high-low)/2;
72+
if(sum+mid>a[n/2]*2*n){
73+
high=mid-1;
74+
}else{
75+
low=mid+1;
76+
}
77+
}
78+
79+
80+
cout<<low<<endl;
81+
}
82+
83+
84+
85+
86+
87+
signed main() {
88+
fast_io();
89+
90+
int t;
91+
cin >> t;
92+
93+
while (t--) {
94+
solve();
95+
}
96+
97+
return 0;
98+
}

0 commit comments

Comments
 (0)