Minimize Array Sum After K Halving Operations
Problem statement
Perform exactly k operations on an array of nonnegative integers. In one operation, choose an element, replace it by ceil(value/2), and put it back.
Return the minimum possible array sum after all operations.
Function
minimumSumAfterHalves(values: int[], k: int) → longExamples
Example 1
values = [10,20,7]k = 4return = 14Repeatedly reducing a current maximum yields the minimum sum.
Example 2
values = [1,1]k = 3return = 2Halving one leaves it at one, so the sum stays two.
Example 3
values = [5]k = 2return = 2Five becomes three, then two.
Constraints
1 <= values.length <= 100000.0 <= values[i] <= 10^9.0 <= k <= 100000.