2856. Minimum Array Length After Pair Removals Medium

@problem@discussion
#Array#Hash Table#Two Pointers#Binary Search#Greedy#Counting



1/**
2 * [2856] Minimum Array Length After Pair Removals
3 *
4 * Given an integer array num sorted in non-decreasing order.
5 * You can perform the following operation any number of times:
6 * 
7 * 	Choose two indices, i and j, where nums[i] < nums[j].
8 * 	Then, remove the elements at indices i and j from nums. The remaining elements retain their original order, and the array is re-indexed.
9 * 
10 * Return the minimum length of nums after applying the operation zero or more times.
11 *  
12 * <strong class="example">Example 1:
13 * <div class="example-block">
14 * Input: <span class="example-io">nums = [1,2,3,4]</span>
15 * Output: <span class="example-io">0</span>
16 * Explanation:
17 * <img src="https://assets.leetcode.com/uploads/2024/05/18/tcase1.gif" style="width: 160px; height: 70px;" />
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">nums = [1,1,2,2,3,3]</span>
22 * Output: <span class="example-io">0</span>
23 * Explanation:
24 * <img src="https://assets.leetcode.com/uploads/2024/05/19/tcase2.gif" style="width: 240px; height: 70px;" />
25 * </div>
26 * <strong class="example">Example 3:
27 * <div class="example-block">
28 * Input: <span class="example-io">nums = [1000000000,1000000000]</span>
29 * Output: <span class="example-io">2</span>
30 * Explanation:
31 * Since both numbers are equal, they cannot be removed.
32 * </div>
33 * <strong class="example">Example 4:
34 * <div class="example-block">
35 * Input: <span class="example-io">nums = [2,3,4,4,4]</span>
36 * Output: <span class="example-io">1</span>
37 * Explanation:
38 * <img src="https://assets.leetcode.com/uploads/2024/05/19/tcase3.gif" style="width: 210px; height: 70px;" />
39 * </div>
40 *  
41 * Constraints:
42 * 
43 * 	1 <= nums.length <= 10^5
44 * 	1 <= nums[i] <= 10^9
45 * 	nums is sorted in non-decreasing order.
46 * 
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/minimum-array-length-after-pair-removals/
51// discuss: https://leetcode.com/problems/minimum-array-length-after-pair-removals/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56    pub fn min_length_after_removals(nums: Vec<i32>) -> i32 {
57        0
58    }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn test_2856() {
69    }
70}
71


Back
© 2025 bowen.ge All Rights Reserved.