You are given an integer array a of length n. You may rearrange the elements in any order.
Define the prefix sums of a permutation p as:
pref[i] = p[0] + p[1] + ... + p[i]
for
0 <= i < n
Let score(p) be the number of indices i such that pref[i] > 0.
Compute the maximum possible value of score(p) over all permutations p of the array.
a
(may contain negative, zero, and positive values).
1 <= n <= 2 * 10^5
-10^9 <= a[i] <= 10^9
O(n log n)
(or better).
[2, -1, 2, -3]
[2, 2, -1, -3]
[2, 4, 3, 0]
→ positive prefix sums =
3
→ Output:
3