Sum of Mutated Array Closest to Target

Try to solve the Sum of Mutated Array Closest to Target problem.

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:

  • 11 \leqarr.length 103 \leq 10^3

  • 11 \leqarr[i], target 104\leq 10^4

Examples

Press + to interact
canvasAnimation-image
1 of 4

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Sum of Mutated Array Closest to Target

1

What value should be chosen if arr = [3, 9, 4] and target = 12?

A)

4

B)

3

C)

5

D)

9

Question 1 of 40 attempted

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding on how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Try it yourself

Implement your solution in the following coding playground. 

Press + to interact
Java
usercode > Solution.java
import java.util.Arrays;
public class Solution{
public static int findBestValue(int[] arr, int target) {
// Replace this placeholder return statement with your code
return -1;
}
}
Sum of Mutated Array Closest to Target

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.