Minimum Deletions to Make Array Beautiful

You are given a 0-indexed integer array nums. The array nums is beautiful if:

  • nums.length is even.
  • nums[i] != nums[i + 1] for all i % 2 == 0.

Note that an empty array is considered beautiful.

You can delete any number of elements from nums. When you delete an element, all the elements to the right of the deleted element will be shifted one unit to the left to fill the gap created and all the elements to the left of the deleted element will remain unchanged.

Return the minimum number of elements to delete from nums to make it beautiful.

Example 1
Inputnums = [1,1,2,3,5]
Output1
You can delete either nums[0] or nums[1] to make nums = [1, 2, 3, 5], which is beautiful, and at least 1 deletion is needed.
Example 2
Inputnums = [1,1,2,2,3,3]
Output2
You can delete nums[0] and nums[5] to make nums = [1, 2, 2, 3], which is beautiful, and at least 2 deletions are needed.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^5

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