Minimum Value to Subtract from Array Elements to Make Sum Equal K
viaGlassdoor
Problem: Given an array of positive integers and a target K, find the minimum value V such that if every array element greater than V is reduced to V (capped at V), the total sum of the array becomes <= K.
Constraints: Array of positive integers; K is a target sum.
Example: arr = [10, 20, 30], K = 30 -> find minimum V such that sum(min(arr[i], V)) <= K.
Approach: Binary search on V (the capping value) over the range [0, max(arr)]. For each candidate V, compute the capped sum in O(n) and check if it's <= K; adjust the binary search bounds accordingly (monotonic: higher V -> higher or equal sum). O(n log(max(arr))) time overall.
asked …