🔄 Apply Operations and Move Zeros  | Java  | C++ | python | Javascript |  O(n)
🔍 Problem BreakdownWe need to modify the array in-place by following two rules:
1️⃣ Merge adjacent equal numbers: If nums[i] == nums[i+1], then double nums[i] a
# 🔍 Problem Breakdown
We need to modify the array in-place by following two rules: 
1️⃣ Merge adjacent equal numbers: If nums[i] == nums[i+1], then double nums[i] and set nums[i+1] = 0.
2️⃣ Move all zeros to the end: Maintain the relative order of non-zero elements.
# 💡 Intuition
We can solve this problem in two simple passes:

First pass ➝ Apply the merging operation on adjacent elements.
Second pass ➝ Shift non-zero elements forward, then fill remaining positions with 0.

# 📝 Approach
1: Merging Adjacent Equal Numbers
Iterate through nums, and when we find nums[i] == nums[i+1], we double nums[i] and set nums[i+1] = 0.
2: Move All Zeros to the End
Traverse the array again, shift all non-zero numbers forward, and replace the rest with 0

# Complexity
⏳ Time Complexity: O(n)
🛠 Space Complexity: O(1) (in-place modification)

# Code
```java []
class Solution {
    public int[] applyOperations(int[] nums) {
        
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }

        // Movinh all zeros to the end while maintaining order
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[index++] = nums[i];
            }
        }
        
        // Filling remaining elements with zero
        while (index < nums.length) {
            nums[index++] = 0;
        }

        return nums;
    }
}

```
```C++ []
class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {

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

        int index = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] != 0) {
                swap(nums[index++], nums[i]);
            }
        }

        return nums;
    }
};
```
```Javascript []
var applyOperations = function(nums) {
    
    for (let i = 0; i < nums.length - 1; i++) {
        if (nums[i] === nums[i + 1]) {
            nums[i] *= 2;
            nums[i + 1] = 0;
        }
    }

    
    let index = 0;
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] !== 0) {
            [nums[index], nums[i]] = [nums[i], nums[index]];
            index++;
        }
    }

    return nums;
};
```
```Python []
class Solution:
    def applyOperations(self, nums):
        for i in range(len(nums) - 1):
            if nums[i] == nums[i + 1]:
                nums[i] *= 2
                nums[i + 1] = 0

       
        index = 0
        for i in range(len(nums)):
            if nums[i] != 0:
                nums[index], nums[i] = nums[i], nums[index]
                index += 1

        return nums


solution = Solution()
print(solution.applyOperations([2, 2, 0, 4, 4, 8])) 

```

['Python', 'C++', 'Java', 'JavaScript']