java.lang.Object
g1201_1300.s1300_sum_of_mutated_array_closest_to_target.Solution

public class Solution extends Object
1300 - Sum of Mutated Array Closest to Target.<p>Medium</p> <p>Given an integer array <code>arr</code> and a target value <code>target</code>, return the integer <code>value</code> such that when we change all the integers larger than <code>value</code> in the given array to be equal to <code>value</code>, the sum of the array gets as close as possible (in absolute difference) to <code>target</code>.</p> <p>In case of a tie, return the minimum such integer.</p> <p>Notice that the answer is not neccesarilly a number from <code>arr</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> arr = [4,9,3], target = 10</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> When using 3 arr converts to [3, 3, 3] which sums 9 and that&rsquo;s the optimal answer.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> arr = [2,3,5], target = 10</p> <p><strong>Output:</strong> 5</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> arr = [60864,25176,27249,21296,20204], target = 56803</p> <p><strong>Output:</strong> 11361</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= arr.length <= 10<sup>4</sup></code></li> <li><code>1 <= arr[i], target <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findBestValue

      public int findBestValue(int[] arr, int target)
    • check

      public int check(int v, int[] arr, int target)