✅ Beginner Friendly | Easy to Understand | Two Pointers | Detailed Video Explanation 🔥
IntuitionThe problem requires modifying an array in two steps:
If two adjacent elements are equal, double the first one and set the second to zero.
Shift all ze
# Intuition
The problem requires modifying an array in two steps:  
1. If two adjacent elements are equal, double the first one and set the second to zero.  
2. Shift all zeroes to the end while maintaining the relative order of non-zero elements.  

This hints at a **two-pass approach**—one for modifying the array and another for reordering the elements.

# Approach
1. **Modify the array**  
   - Iterate through the array and check if two adjacent elements are equal.  
   - If they are, double the first element and set the second to zero.  

2. **Shift non-zero elements to the left**  
   - Use two pointers:  
     - One pointer (`i`) keeps track of the position where the next non-zero element should go.  
     - Another pointer (`j`) iterates through the array.  
   - If `nums[j]` is nonzero, place it at `nums[i]` and increment `i`.  

3. **Fill the remaining positions with zero**  
   - After placing all non-zero elements, fill the rest of the array with zeroes.  

# Complexity
- **Time complexity:**  
  - The first loop runs in **O(n)** time.  
  - The second loop (shifting elements) also runs in **O(n)** time.  
  - The final loop (filling zeros) runs in **O(n)** time in the worst case.  
  - **Overall complexity:** $$O(n)$$  

- **Space complexity:**  
  - We modify the array in place, using only a few extra variables.  
  - **Overall complexity:** $$O(1)$$  


# Code
```java []
class Solution {
    public int[] applyOperations(int[] nums) {
        int n = nums.length;
        
        // Step 1: Modify the array by doubling adjacent equal elements and setting the next one to zero
        for(int i = 0; i <= n - 2; i++){
            if(nums[i] == nums[i+1]){
                nums[i] *= 2;  // Double the current element
                nums[i+1] = 0; // Set the next element to zero
            }
        }
        
        // Step 2: Shift all non-zero elements to the left while maintaining their relative order
        int i = 0, j = 0;
        for(; j < n; j++){
            if(nums[j] > 0){
                nums[i++] = nums[j]; // Move non-zero elements to the front
            }
        }
        
        // Step 3: Fill the remaining positions with zeroes
        while(i < n) nums[i++] = 0;

        return nums;
    }
}

```

https://youtu.be/9_Bq597dykw
['Array', 'Two Pointers', 'Simulation', 'Java']