C++ Two Pointers + Greedy O(N) Time O(1) Space
See my latest update in repo LeetCode\n\n## Solution 1. Two Pointers + Greedy\n\nWe can greedily find the even and odd pairs from left to right.\n\nWhy greedy? 
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1. Two Pointers + Greedy\n\nWe can greedily find the even and odd pairs from left to right.\n\n**Why greedy**? Think if the first two elements, if they are equal, we have to delete one of them -- deleting the rest of the numbers won\'t change anything to these first two equal numbers. So, when we see two equal numbers, we have to delete one of them, until we find two non-equal numbers.\n\nUse two pointers `i` the write pointer and `j` the read pointer.\n\nWe keep the following process until `j` exhausts the array.\n* Write `A[j]` to the even-indexed `A[i]` and increment both pointers.\n* Try to find the odd-indexed value. Keep skipping/deleting if `A[j] == A[i-1]`.\n* If found a valid odd-indexed value, write `A[j]` to `A[i]` and increment both pointers.\n\nLastly, if the resultant array has an odd length (i.e. `i % 2 != 0`), we delete the last element.\n\n```cpp\n// OJ: https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1) extra space\nclass Solution {\npublic:\n    int minDeletion(vector<int>& A) {\n        int i = 0, N = A.size(), j = 0, ans = 0;\n        while (j < N) {\n            A[i++] = A[j++]; // Write `A[j]` to the even-indexed `A[i]`, increment both pointers\n            while (j < N && A[i - 1] == A[j]) ++j, ++ans; // Trying to find the odd-indexed value. Keep skipping/deleting if `A[j] == A[i-1]`\n            if (j < N) A[i++] = A[j++]; // If found, write `A[j]` to `A[i]` and increment both pointers\n        }\n        if (i % 2) ++ans; // If the resultant array is of odd length, delete the last element\n        return ans;\n    }\n};\n```\n\n## Solution 2. Greedy\n\nSimilar to Solution 1, just find non-equal pairs `A[i]` and `A[i + 1]`. If `A[i] == A[i + 1]`, delete `A[i]` and `++i`.\n\nIf in the end, there is no such pair, delete `A[i]`.\n\n```cpp\n// OJ: https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1)\nclass Solution {\npublic:\n    int minDeletion(vector<int>& A) {\n        int i = 0, N = A.size(), ans = 0;\n        for (; i < N; i += 2) {\n            while (i + 1 < N && A[i] == A[i + 1]) ++i, ++ans; // delete `A[i]`\n            if (i + 1 == N) ++ans; // can\'t find a pair, delete `A[i]`\n        }\n        return ans;\n    }\n};\n```
[]