FastPrepSum of Mutated Array Closest to Target

Sum of Mutated Array Closest to Target

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

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) → int

Examples

Example 1

nums = [4,9,3]target = 10return = 3

Cap 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.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int findBestValue(int[] nums, int target) {
  // Write your code here.
}
nums[4,9,3]
target10
expected3
Checking account…