Problem · Array

Minimum Removals to Balance Array

MediumSalesforceOA
See Salesforce hiring insights

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.

Examples
01 · Example 1
arr = [7, 4, 2, 3, 12, 9]
return = 2

An optimal sequence of operations is:

  • Change the second element from 4 to 8.
  • Remove the third and fourth elements, 2 and 3.
    • The modified array is [7, 8, 12, 9].
    • 12 is less than or equal to 2 * 7.
02 · Example 2
arr = [4, 6, 2, 9, 8, 7, 3]
return = 2

An optimal sequence of operations is:

  • Change the third element from 2 to 11.
  • Remove the first and seventh elements, 4 and 3.
    • The modified array is [6, 11, 9, 8, 7].
    • 11 is less than or equal to 2 * 6.
More Salesforce problems
drafts saved locally
public int minimumRemovalsToBalance(int[] arr) {
  // write your code here
}
arr[7, 4, 2, 3, 12, 9]
expected2
checking account