🔥Beats 100% |✨Easy Approach | Easy for Beginners | C++ | Java
IntuitionWe need to modify the given array based on certain operations:
- If two consecutive elements are equal, double the first element and set the second ele

![Screenshot 2025-03-01 194756.png](https://assets.leetcode.com/users/images/bca1620b-8c92-49d5-9f55-b64bb9c5176d_1740839261.3764608.png)


# Intuition
We need to modify the given array based on certain operations:
    - If two consecutive elements are equal, double the first element and set the second element to 0.
    - Ahift all zero elements to the right of the array.

****

# Approach
- The loop iterates through the array for index `0` to `n - 1`.
- It checks if two consecutive elements `nums[i]` and `nums[i + 1]` are equal or not.
    - If equal, it doubles `nums[i]` and sets `nums[i + 1]` to 0.
    - If not equal, it continues to next iteration.
- We another loop to swap the elements with zeroes and move all the zeroes to right while maintaining order of non-zer0 elements.

****

# Complexity
- **Time complexity:**
    - **O(N)** -> To iterate over all elements in the given array.

- **Space complexity:**
    - **O(1)** -> No extra space is used.

****

# Code
```cpp []
class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {
        int n = nums.size(), idx = 0;

        for(int i = 0; i < n - 1; i++){
            if(nums[i] == nums[i + 1]){
                nums[i] = nums[i] * 2;
                nums[i + 1] = 0;
            }
        }

        for(int i = 0; i < n; i++){
            if(nums[i] != 0){
                swap(nums[i], nums[idx]);
                idx++;
            }
        }

        return nums;
    }
};
```
```java []
class Solution {
    public int[] applyOperations(int[] nums) {
        int n = nums.length, idx = 0;

        for(int i = 0; i < n - 1; i++){
            if(nums[i] == nums[i + 1]){
                nums[i] = nums[i] * 2;
                nums[i + 1] = 0;
            }
        }

        for(int i = 0; i < n; i++){
            if(nums[i] != 0){
                int temp = nums[i];
                nums[i] = nums[idx];
                nums[idx] = temp;
                idx++;
            }
        }

        return nums;
    }
}
```
['Array', 'Simulation', 'C++', 'Java']