Minimum Array Length After Pair Removals

Given an integer array nums sorted in non-decreasing order.

You can perform the following operation any number of times:

  • Choose two indices, i and j, where nums[i] < nums[j].
  • Then, remove the elements at indices i and j from nums. The remaining elements retain their original order, and the array is re-indexed.

Return the minimum length of nums after applying the operation zero or more times.

Example 1
Inputnums = [1,2,3,4]
Output0
All elements can be removed by repeatedly pairing a smaller number with a larger number.
Example 2
Inputnums = [1,1,2,2,3,3]
Output0
All elements can be removed by pairing each smaller occurrence with a larger occurrence.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • nums is sorted in non-decreasing order.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate