Both O(n) and O(1) space complexity Java solution
First we will see the O(n) space complexity solution then we will move forward to the optimized one\nThe only reason I\'m telling you both solutions is to get t
First we will see the O(n) space complexity solution then we will move forward to the optimized one\nThe only reason I\'m telling you both solutions is to get the intution behind it So make sure you read the whole post.\nFirst approach:\nIn this approach simply convert the array into arraylist\nand do what condition given in the question says\n\nTraverse through the arraylist using while loop and if at even index the ith element is equal to (i+1)th element then \nremove the ith element and increment the count.\nand just get back to previous index\nafter traversing the whole arraylist\njust make sure your arraylist length is even and if it\'s not then do count++\n```\nclass Solution {\n    public int minDeletion(int[] nums) {\n        ArrayList<Integer> num = new ArrayList<>();\n        for(int i = 0; i < nums.length; i++){\n            num.add(nums[i]);\n        }\n        int count = 0;\n        int i = 0;\n        while(i < num.size() - 1){\n            if(i%2 == 0 && num.get(i).equals(num.get(i+1))){\n                num.remove(i);\n                i= i - 1;\n                count++;\n            }\n            i++;\n        }\n        if(num.size() % 2 != 0){\n            count++;\n        }\n        return count;\n    }\n}\n```\nSo, here time complexity will be O(n)\nand space complexity will also be O(n)\n\nNote: don\'t use == operator for comparison of element as you will get WA.\n\nOptimised approach:\n\nNow here I want you to imagine the above solution without arraylist\nthen probably you don\'t have to remove the elements right?\nand since we have to check only even elements so we can directly jump to i+2 elements\nbut what about length of array\nhow we make check for that without removing elements?\nIt\'s simple just check if array length - count is even or not is it is odd then increment the count\nIt was simple right?\n```\nclass Solution {\n    public int minDeletion(int[] nums) {\n        int count = 0;\n        int i = 0;\n        while(i < nums.length - 1){\n            if(nums[i] == nums[i + 1]){\n                count++;\n                i--;\n            }\n            i += 2;\n        }\n        if((nums.length - count) % 2 != 0){\n            count++;\n        }\n        return count;\n    }\n}\n```\nTC: O(n)\nSC: O(1)\n\n
[]