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
Visualization
Time & Space Complexity
Single pass to count frequencies, then one pass through frequency map
Hash map to store frequency of each unique element
Constraints
- 1 โค nums.length โค 105
- 1 โค nums[i] โค 109
- nums is sorted in non-decreasing order