3634. Minimum Removals to Balance Array Medium

@problem@discussion
#Array#Binary Search#Sliding Window#Sorting



1/**
2 * [3634] Minimum Removals to Balance Array
3 *
4 * You are given an integer array nums and an integer k.
5 * An array is considered balanced if the value of its maximum element is at most k times the minimum element.
6 * You may remove any number of elements from nums​​​​​​​ without making it empty.
7 * Return the minimum number of elements to remove so that the remaining array is balanced.
8 * Note: An array of size 1 is considered balanced as its maximum and minimum are equal, and the condition always holds true.
9 *  
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">nums = [2,1,5], k = 2</span>
13 * Output: <span class="example-io">1</span>
14 * Explanation:
15 * 
16 * 	Remove nums[2] = 5 to get nums = [2, 1].
17 * 	Now max = 2, min = 1 and max <= min * k as 2 <= 1 * 2. Thus, the answer is 1.
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">nums = [1,6,2,9], k = 3</span>
22 * Output: <span class="example-io">2</span>
23 * Explanation:
24 * 
25 * 	Remove nums[0] = 1 and nums[3] = 9 to get nums = [6, 2].
26 * 	Now max = 6, min = 2 and max <= min * k as 6 <= 2 * 3. Thus, the answer is 2.
27 * </div>
28 * <strong class="example">Example 3:
29 * <div class="example-block">
30 * Input: <span class="example-io">nums = [4,6], k = 2</span>
31 * Output: <span class="example-io">0</span>
32 * Explanation:
33 * 
34 * 	Since nums is already balanced as 6 <= 4 * 2, no elements need to be removed.
35 * </div>
36 *  
37 * Constraints:
38 * 
39 * 	1 <= nums.length <= 10^5
40 * 	1 <= nums[i] <= 10^9
41 * 	1 <= k <= 10^5
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-removals-to-balance-array/
47// discuss: https://leetcode.com/problems/minimum-removals-to-balance-array/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn min_removal(nums: Vec<i32>, k: i32) -> i32 {
53        0
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_3634() {
65    }
66}
67

Back
© 2026 bowen.ge All Rights Reserved.