[LeetCode] Minimum Removals to Balance Array

3634. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class Solution {
public:
int minRemoval(vector<int>& nums, long long k) {
int res = INT_MAX, n = nums.size();
sort(begin(nums), end(nums));
for(int i = 0, j = 0; i < n; i++) {
if(i and nums[i] == nums[i-1]) continue;
while(j < n and nums[i] * k >= nums[j]) j++;
res = min(res, i + n - j);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/08/03/PS/LeetCode/minimum-removals-to-balance-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.