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.

&nbsp;
Example 1:

Input: nums = [1,1,2,3,5]
Output: 1
Explanation: You can delete either nums[0] or nums[1] to make nums = [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make nums beautiful.


Example 2:

Input: nums = [1,1,2,2,3,3]
Output: 2
Explanation: You can delete nums[0] and nums[5] to make nums = [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	0 &lt;= nums[i] &lt;= 105


class Solution:
    def minDeletion(self, nums: List[int]) -> int:
        # Greedy !
        # we first only consider requirement 2: nums[i] != nums[i + 1] for all i % 2 == 0
        # at the begining, we consider the num on the even index
        # when we delete a num, we need consider the num on the odd index
        # then repeat this process
        # at the end we check the requirement 1: nums.length is even or not
        
        n = len(nums)
        count = 0
        # flag is true then check the even index
        # flag is false then check the odd index
        flag = True
        
        for i in range(n):
            # check the even index
            if flag:
                if i % 2 == 0 and i != n -1 and nums[i] == nums[i + 1]:
                    count += 1
                    flag = False
            # check the odd index
            elif not flag:
                if i % 2 == 1 and i != n -1 and nums[i] == nums[i + 1]:
                    count += 1
                    flag = True
        
        curLength = n - count
        
        return count if curLength % 2 == 0 else count + 1
class Solution {
    public int minDeletion(int[] nums) {

        int deletion = 0, n = nums.length;

        for (int i=0; i<n-1; ) {
            int newIndex = i-deletion;
            if ((newIndex % 2 == 0) && nums[i] == nums[i+1]) deletion++;
            else i++;
        }
        return ((n-deletion) % 2 == 0) ? deletion : deletion+1;
    }
}
INTUITION
1. since we have to find the minimum deletions, we dont have to 
	actually delete the elements , we just have to count those elements.
2. Now if we delete the element and shift all the elements towards left , it will
	cause time limit exceeded.
3. To handle above case we can  observe one thing that, if we delete some element
	at a certain position then the indices of all the elements towards the right 
	will get inverted means the odd index will become even and the even will become odd.
4. In the end checking if the vector size if even or odd, if it is even simply return or else if it 
	is odd then decrement result by 1 since we have to remove one element to have the vector size even.
class Solution {
public:
    int minDeletion(vector<int>& nums) {
        int res=0,n=nums.size(),i;
        int flag=1;
            for(i=0;i<nums.size()-1;i++){
                    if(i%2==0 and nums[i]==nums[i+1]  and flag){
                        res++;
                        flag=0;
                    }
                    else if(i%2==1 and nums[i]==nums[i+1] and flag==0){
                        res++;
                        flag=1;
                    }
                }
        int x=n-res;
        if(x%2==0){
            return res;
        }
        return res+1;
    }
};
var minDeletion = function(nums) {
    const n = nums.length;
    const res = [];

    for (let i = 0; i < n; ++i) {
        const num = nums[i];

        if (res.length % 2 === 0 || res.at(-1) != num) {
            res.push(num);
        }
    }

    if (res.length % 2 === 1) res.pop();

    return n - res.length;
};
Minimum Deletions to Make Array Beautiful