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+ ## Approach
2+ - Sort the wealth array
3+ - Target the middle index to ensure more than half are unhappy
4+ - Solve inequality directly instead of using binary search
5+
6+ ## Complexity
7+ - Time: O(n log n)
8+ - Space: O(n)
Original file line number Diff line number Diff line change 1+ ## C. Robin Hood in Town
2+
3+ Link: https://codeforces.com/contest/2014/problem/C
4+
5+ Determine the minimum extra gold required so that
6+ strictly more than half of the population becomes unhappy.
Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .readline
3+
4+ t = int (input ())
5+
6+ for _ in range (t ):
7+ n = int (input ())
8+ a = list (map (int , input ().split ()))
9+
10+ if n <= 2 :
11+ print (- 1 )
12+ continue
13+
14+ a .sort ()
15+ total = sum (a )
16+
17+ k = n // 2
18+ x = 2 * n * a [k ] - total + 1
19+
20+ print (max (0 , x ))
You can’t perform that action at this time.
0 commit comments