↓Skip to main content
  1. LeetCode/

LeetCode 3634: Minimum Removals to Balance Array

·2 mins· ·
LeetCode Medium Sliding-Window Sorting Binary-Search Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-10

  • Submissions: Accepted ×3 (written on 2026-01-10, 02-06 and 02-10)

Approach
#

Only the max and min matter, not the order, so sort first.

After sorting, the kept elements are always a contiguous block: if a and c are kept, any b between them never makes the condition worse.

For each nums[left] as the minimum, find the farthest right with nums[right] <= nums[left] * k. The threshold only grows with left, so right never moves back. The removals are left + (n - right).

My Solution
#

class Solution:
    def minRemoval(self, nums: List[int], k: int) -> int:
        nums.sort()
        n = len(nums)
        best = n
        right = 0
        for left in range(n):
            while right < n and nums[right] <= nums[left] * k:
                right += 1                         # [left, right) can all stay
            best = min(best, left + (n - right))
        return best

Complexity
#

Time O(n log n), space O(1) apart from sorting.

Mistakes to Avoid
#

  • Looking for a contiguous block without sorting: any elements may be removed, not just a contiguous run.

Interview Explanation
#

The condition depends only on the max and min, so I sort, and then the kept elements form a contiguous block. I try each nums[left] as the minimum and move a right-only pointer to the farthest element that can stay, removing everything to the left and right, and take the minimum. Sorting dominates: O(n log n).

Related

LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 1208: Get Equal Substrings Within Budget
·1 min
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 1208 note: each position costs |s[i] - t[i]|, so the problem becomes the longest subarray with sum <= maxCost
LeetCode 2024: Maximize the Confusion of an Exam
·2 mins
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 2024 note: a window is valid when the rarer of T and F appears at most k times (flip all of them); shrink the left when it is not
LeetCode 2958: Length of Longest Subarray With at Most K Frequency
·1 min
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2958 note: a longest-window slide that shrinks the left whenever the newly added number appears more than k times
LeetCode 2653: Sliding Subarray Beauty
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2653 note: values are only -50 to 50, so the fixed window keeps a count array, and each window accumulates upward from -50 to find the x-th smallest negative
LeetCode 220: Contains Duplicate III
·3 mins
LeetCode Hard Sliding-Window Bucket-Sort Ordered-Set Sorting Array
LeetCode 220 note: with bucket size valueDiff + 1, two numbers in one bucket always qualify, so only the two neighboring buckets need checking; the window keeps the latest indexDiff numbers, so each bucket holds at most one