✅ [Python/C++] use the NOT trick to move zeros (explained)
\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.\n*\nThis solution employs a brute force approach to perform operations along with (stable-)sorting using a *log
**\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\nThis solution employs a brute force approach to perform operations along with (stable-)sorting using a **logical not** trick to move zeros to the end of array. Time complexity is linear: **O(N\\*logN)**. Space complexity is linear: **O(N)**.\n\n\n**Python.**\n```\nclass Solution:\n    def applyOperations(self, n):\n\n        # apply the first part of operations\n        for i in range(1,len(n)):\n            if n[i] == n[i-1]:\n                n[i-1], n[i] = n[i-1]*2, 0    \n        \n        # \'not\' of any non-zero number is equal to 0, i.e.,\n        # less than \'not 0\' which is 1 (here, sorting is stable)\n        return sorted(n, key=lambda x: not x)\n```\n\n**C++.**\n```\nclass Solution \n{\npublic:\n    vector<int> applyOperations(vector<int>& nums) \n    {\n        for (int i = 1; i < nums.size(); ++i)\n            if (nums[i] == nums[i-1])\n                nums[i-1] *= 2, nums[i] = 0;\n        \n\t\t// stable sort is important, it keeps the original order of non-zero numbers\n        stable_sort(nums.begin(), nums.end(), [](int a, int b) { return !a < !b; });\n        return nums;\n    }\n};\n```
[]