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
More Salesforce problems
- Diameter of an Acyclic Undirected GraphONSITE INTERVIEW · Seen Jul 2026
- Optimal Account BalancingPHONE SCREEN · Seen Jul 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Jul 2026
- Maximal SquarePHONE SCREEN · Seen Jul 2026
- Maximum Barbell WeightOA · Seen Jul 2026
- Minimum No-Repeat Segments After One Character RemovalOA · Seen Jul 2026
- Minimum Operations to ZeroOA · Seen Jul 2026
- Minimize Total Input Cost (for LTMS)OA · Seen Jun 2026