File tree Expand file tree Collapse file tree
Problems/Binary-Search/Day-06/sol/ayush2005k Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ))
You can’t perform that action at this time.
0 commit comments