Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
Input: nums = [2,1,5], k = 2
Output: 1
Explanation:
nums[2] = 5 to get nums = [2, 1].max = 2, min = 1 and max <= min * k as 2 <= 1 * 2. Thus, the answer is 1.Example 2:
Input: nums = [1,6,2,9], k = 3
Output: 2
Explanation:
nums[0] = 1 and nums[3] = 9 to get nums = [6, 2].max = 6, min = 2 and max <= min * k as 6 <= 2 * 3. Thus, the answer is 2.Example 3:
Input: nums = [4,6], k = 2
Output: 0
Explanation:
nums is already balanced as 6 <= 4 * 2, no elements need to be removed.Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1091 <= k <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Sliding Window
[2,1,5] 2
[1,6,2,9] 3
[4,6] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3634: Minimum Removals to Balance Array
class Solution {
public int minRemoval(int[] nums, int k) {
Arrays.sort(nums);
int cnt = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
int j = n;
if (1L * nums[i] * k <= nums[n - 1]) {
j = Arrays.binarySearch(nums, nums[i] * k + 1);
j = j < 0 ? -j - 1 : j;
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
}
// Accepted solution for LeetCode #3634: Minimum Removals to Balance Array
func minRemoval(nums []int, k int) int {
sort.Ints(nums)
n := len(nums)
cnt := 0
for i := 0; i < n; i++ {
j := n
if int64(nums[i])*int64(k) <= int64(nums[n-1]) {
target := int64(nums[i])*int64(k) + 1
j = sort.Search(n, func(x int) bool {
return int64(nums[x]) >= target
})
}
cnt = max(cnt, j-i)
}
return n - cnt
}
# Accepted solution for LeetCode #3634: Minimum Removals to Balance Array
class Solution:
def minRemoval(self, nums: List[int], k: int) -> int:
nums.sort()
cnt = 0
for i, x in enumerate(nums):
j = bisect_right(nums, k * x)
cnt = max(cnt, j - i)
return len(nums) - cnt
// Accepted solution for LeetCode #3634: Minimum Removals to Balance Array
impl Solution {
pub fn min_removal(mut nums: Vec<i32>, k: i32) -> i32 {
nums.sort();
let mut cnt = 0;
let n = nums.len();
for i in 0..n {
let mut j = n;
let target = nums[i] as i64 * k as i64;
if target <= nums[n - 1] as i64 {
j = nums.partition_point(|&x| x as i64 <= target);
}
cnt = cnt.max(j - i);
}
(n - cnt) as i32
}
}
// Accepted solution for LeetCode #3634: Minimum Removals to Balance Array
function minRemoval(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let cnt = 0;
for (let i = 0; i < n; ++i) {
let j = n;
if (nums[i] * k <= nums[n - 1]) {
const target = nums[i] * k + 1;
j = _.sortedIndexBy(nums, target, x => x);
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.