Minimum Removals to Balance Array

You are given an integer array nums and an integer k.

An array is considered balanced if the value of its maximum element is at most k times the minimum element.

You may remove any number of elements from nums without making it empty.

Return the minimum number of elements to remove so that the remaining array is balanced.

Note: An array of size 1 is considered balanced as its maximum and minimum are equal, and the condition always holds true.

Example 1
Inputnums = [2,1,5], k = 2
Output1
Remove nums[2] = 5 to get nums = [2, 1], where max = 2, min = 1, and 2 <= 1 * 2, so the answer is 1.
Example 2
Inputnums = [1,6,2,9], k = 3
Output2
Remove nums[0] = 1 and nums[3] = 9 to get nums = [6, 2], where max = 6, min = 2, and 6 <= 2 * 3, so the answer is 2.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 1 <= k <= 10^5

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate