Search⌘ K
AI Features

Sum of Mutated Array Closest to Target

Explore how to solve the problem of mutating array elements so their sum is nearest to a target value. Learn to apply sorting and binary search strategies to efficiently determine the integer value that, when replacing larger elements, minimizes the difference and handles ties by choosing the smaller candidate.

Statement

Given an integer array arr and a target value target, find an integer value such that if all the numbers in arr greater than value are replaced with a value, the sum of the array gets as close as possible to the target.

Choose the smaller value if there’s a tie (two value options are equally close to the targe).

Note: The answer doesn’t have to be a number from the array.

Constraints:

  • ...