Sum of Mutated Array Closest to Target
Problem statement
Choose a nonnegative integer cap. Replace every nums[i] > cap by cap and leave smaller values unchanged. Return the cap whose mutated-array sum is closest to target; break ties by smaller cap.
Function
findBestValue(nums: int[], target: int) → intExamples
Example 1
nums = [4,9,3]target = 10return = 3Cap 3 gives sum 9, closer than cap 4's sum 11; the distances tie, so choose 3.
Constraints
1 <= nums.length <= 10^5.- Values and target are positive integers.