3634. Minimum Removals to Balance Array
Description
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:
- Remove
nums[2] = 5to getnums = [2, 1]. - Now
max = 2,min = 1andmax <= min * kas2 <= 1 * 2. Thus, the answer is 1.
Example 2:
Input: nums = [1,6,2,9], k = 3
Output: 2
Explanation:
- Remove
nums[0] = 1andnums[3] = 9to getnums = [6, 2]. - Now
max = 6,min = 2andmax <= min * kas6 <= 2 * 3. Thus, the answer is 2.
Example 3:
Input: nums = [4,6], k = 2
Output: 0
Explanation:
- Since
numsis already balanced as6 <= 4 * 2, no elements need to be removed.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1091 <= k <= 105
Solutions
Solution 1: Sorting + Binary Search
Thinking
The remainder is balanced iff its maximum is at most \(k\) times its minimum. Deletion is equivalent to keeping a contiguous segment after sorting. Enumerating subsets fails at \(n\le 10^5\).
After sorting, if \(i\) is the left end (the minimum), the right end cannot exceed \(k\cdot \textit{nums}[i]\). Binary search finds the first index \(j\) past that bound; \([i,j)\) may be kept.
Track the longest window; the answer is \(n\) minus that length. Sorting makes the extrema of a window its two ends.
We first sort the array, then enumerate each element \(\textit{nums}[i]\) from small to large as the minimum value of the balanced array. The maximum value \(\textit{max}\) of the balanced array must satisfy \(\textit{max} \leq \textit{nums}[i] \times k\). Therefore, we can use binary search to find the index \(j\) of the first element greater than \(\textit{nums}[i] \times k\). At this point, the length of the balanced array is \(j - i\). We record the maximum length \(\textit{cnt}\), and the final answer is the array length minus \(\textit{cnt}\).
The time complexity is \(O(n \times \log n)\), and the space complexity is \(O(\log n)\), where \(n\) is the length of the array \(\textit{nums}\).
1 2 3 4 5 6 7 8 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Solution 2: Sorting + Two Pointers
Thinking
The previous method binary-searches every left end and pays an extra \(\log n\). The feasible right end is monotone in the left end, so two pointers replace the searches.
Advance \(r\) while \(\textit{nums}[r]\le \textit{nums}[l]\cdot k\). Each increment of \(l\) only moves \(r\) rightward. The answer is the minimum of \(n-(r-l)\).
Sorting remains; the scan is linear and avoids overflow handling around a binary-search bound.
We first sort the array, then use two pointers to maintain a sliding window. The left pointer \(l\) enumerates each element \(\textit{nums}[l]\) from left to right as the minimum value of the balanced array. The right pointer \(r\) keeps moving right until \(\textit{nums}[r]\) is greater than \(\textit{nums}[l] \times k\). At this point, the length of the balanced array is \(r - l\), and the number of elements to be removed is \(n - (r - l)\). We record the minimum number of removals as the answer.
The time complexity is \(O(n \times \log n)\) and the space complexity is \(O(\log n)\), where \(n\) is the length of the array \(\textit{nums}\).
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |