Skip to content

Commit addac74

Browse files
authored
Merge pull request #449 from ayush2005k/ayush2005k
Ayush2005k
2 parents c96ba9a + c2c8b8f commit addac74

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

  • Problems/Binary-Search/Day-06/sol/ayush2005k
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Problem: C. Robin Hood in Town
3+
Link: https://codeforces.com/contest/2014/problem/C
4+
5+
Short Problem Statement:
6+
There are n people with given wealth.
7+
The richest person finds extra gold.
8+
A person is unhappy if their wealth is less than half of the average.
9+
Find the minimum gold required so that more than half the population becomes unhappy.
10+
11+
Approach:
12+
- If n <= 2, it is impossible.
13+
- Sort the wealth array.
14+
- To make strictly more than n/2 people unhappy, it is sufficient to
15+
make the person at index n//2 unhappy (0-based).
16+
- Derive the inequality directly and compute the minimum required gold.
17+
18+
Time Complexity:
19+
O(n log n)
20+
21+
Space Complexity:
22+
O(n)
23+
24+
Example:
25+
Input:
26+
4
27+
1 2 3 4
28+
29+
Output:
30+
15
31+
32+
Submission Link:
33+
https://codeforces.com/contest/2014/submission/356103335
34+
"""
35+
36+
import sys
37+
input = sys.stdin.readline
38+
39+
t = int(input())
40+
41+
for _ in range(t):
42+
n = int(input())
43+
a = list(map(int, input().split()))
44+
45+
if n <= 2:
46+
print(-1)
47+
continue
48+
49+
a.sort()
50+
total = sum(a)
51+
52+
k = n // 2
53+
x = 2 * n * a[k] - total + 1
54+
55+
print(max(0, x))

0 commit comments

Comments
 (0)