EASY C++ CODE WITH GOOD/DETAILED EXPLANATION
IntuitionWhen I first saw this problem, I recognized it had two distinct parts:
Apply the doubling operation on equal adjacent elements
Move all resulting zeroe
# Intuition
When I first saw this problem, I recognized it had two distinct parts:
1. Apply the doubling operation on equal adjacent elements
1. Move all resulting zeroes to the end while preserving the order of other elements

The natural approach is to handle these operations sequentially rather than trying to combine them.

---



# Approach
My solution uses a two-step approach:
1. First, I iterate through the array and apply the doubling operation:
- When two adjacent elements are equal, I double the first and set the second to zero
- This creates zeroes throughout the array wherever equal elements were found

2. Then, I use a separate function to move all zeroes to the end:
- I maintain a pointer count that tracks where the next non-zero element should go
- As I iterate through the array, whenever I find a non-zero element, I swap it to position count and increment count
- This effectively collects all non-zero elements at the front while pushing zeroes to the end



This approach is efficient because it only requires two passes through the array and modifies the array in-place without requiring extra space for a new array.

---



# Complexity
- Time complexity:
O(N)

- Space complexity:
O(1)

---


# Code
```cpp []
class Solution {
public:
   // Helper function to move all zeroes to the end while preserving the order of non-zero elements
   void shiftZeroesToEnd(vector<int>& nums) {
      int count = 0;  // Keep track of position where next non-zero element should go
    
      // Iterate through array
      for (int i = 0; i < nums.size(); i++) {
           // If current element is non-zero
           if (nums[i] != 0) {
               // Move it to the position tracked by count
               swap(nums[i], nums[count]);
               // Increment position for next non-zero element
               count++;
           }
           // If element is zero, we skip it (don't increment count)
           // This effectively pushes zeroes to the end
       }
   }

   vector<int> applyOperations(vector<int>& nums) {
       // Apply the operation: if two adjacent elements are equal,
       // double the first one and set the second one to zero
       for(int i = 0; i < nums.size() - 1; i++) {
           if(nums[i] == nums[i+1]) {
               nums[i] *= 2;      // Double the current element
               nums[i+1] = 0;     // Set next element to zero
           }
       }
       
       // Move all zeroes to the end while maintaining the order of non-zero elements
       shiftZeroesToEnd(nums);

       // Return the modified array
       return nums;
   }
};
```
['C++']