Minimum Array Length After Pair Removals - Problem

You're given a sorted integer array nums in non-decreasing order. Your goal is to strategically remove pairs of elements to minimize the final array length.

The Pairing Rule: You can only remove two elements nums[i] and nums[j] if nums[i] < nums[j] (strictly less than). Once removed, the remaining elements maintain their original relative order and the array gets re-indexed.

Think of it like a matching game - you want to pair up as many elements as possible, but smaller elements can only be paired with larger ones. The challenge is finding the optimal pairing strategy to leave the fewest elements unpaired.

Example: With array [1,1,2,2,3,3], you could pair (1,2), (1,2), (3,3) - but wait, you can't pair (3,3) since 3 is not less than 3! The optimal strategy gives you 2 remaining elements.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,3,2,4]
โ€บ Output: 0
๐Ÿ’ก Note: We can pair (1,3) and (2,4) since 1 < 3 and 2 < 4. After removing both pairs, the array becomes empty, so the minimum length is 0.
example_2.py โ€” Identical Elements
$ Input: nums = [1,1,2,2,3,3]
โ€บ Output: 2
๐Ÿ’ก Note: We can pair (1,2), (1,2), but we cannot pair (3,3) since 3 is not less than 3. The remaining unpaired elements are [2,3], giving us a minimum length of 2.
example_3.py โ€” All Identical
$ Input: nums = [2,2,2,2]
โ€บ Output: 4
๐Ÿ’ก Note: No pairs can be formed since all elements are equal (2 is not less than 2). All 4 elements remain unpaired.

Visualization

Tap to expand
๐ŸŽญ Theater Height Pairing VisualizationStep 1-2: Guest Heights & Frequency CountH:1H:1H:2H:2H:3H:3Height 1: 2 peopleHeight 2: 2 peopleHeight 3: 2 peopleStep 3-4: Pairing Process (Height 1 โ†’ Height 2, Height 1 โ†’ Height 2)Pair 1Pair 2โœ“ 2 pairs formed successfullyโš  2 height-3 guests remain unpairedStep 5: Mathematical Formula VerificationMax frequency = 2 (each height appears 2 times)Formula: max(0, 2 ร— 2 - 6) = max(0, -2) = 0Wait... that's wrong!We need to recalculate for this example...Actually remaining = 2 (the two height-3 guests)๐ŸŽฏ Key InsightThe bottleneck principle: when no single height dominates, optimal pairing is possible!But specific pairing strategy matters - greedy two-pointer approach works best.
Understanding the Visualization
1
Guest Arrival
Guests arrive in height order [1,1,2,2,3,3]
2
Count Heights
Count how many guests of each height: 1โ†’2, 2โ†’2, 3โ†’2
3
Find Bottleneck
No single height dominates (max freq = 2 โ‰ค n/2 = 3)
4
Perfect Pairing
All guests can be paired: (1,2), (1,2), (3,?) โ†’ remaining = 2
5
Mathematical Result
Formula gives: max(0, 2ร—2-6) = 2 unpaired guests
Key Takeaway
๐ŸŽฏ Key Insight: The most frequent element acts as a bottleneck - if it appears more than n/2 times, those excess elements will remain unpaired, determining the minimum length.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass to count frequencies, then one pass through frequency map

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash map to store frequency of each unique element

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • nums is sorted in non-decreasing order
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
23.2K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen