Problem · Array
Minimum Removals to Balance Array
Learn this problemProblem statement
You are given an integer array arr.
The array is called balanced if:
- the largest element is at most twice the smallest element
You may perform the following changes:
- Remove any number of elements
- Modify at most one remaining element to any positive integer
Your task is to determine the minimum number of elements that must be removed so that the resulting array can be made balanced under these rules.
Function
minimumRemovalsToBalance(arr: int[]) → intExamples
Example 1
arr = [7, 4, 2, 3, 12, 9]return = 2An optimal sequence of operations is:
- Change the second element from
4to8. - Remove the third and fourth elements,
2and3.- The modified array is
[7, 8, 12, 9] 12is less than or equal to2 * 7.
- The modified array is
Example 2
arr = [4, 6, 2, 9, 8, 7, 3]return = 2An optimal sequence of operations is:
- Change the third element from
2to11 - Remove the first and seventh elements,
4and3.- The modified array is
[6, 11, 9, 8, 7] 11is less than or equal to2 * 6.
- The modified array is