Beats 100% | Array | Two Pointers | Solution for LeetCode#2460
IntuitionThe problem requires applying a specific operation to an array and then moving all non-zero elements to the left while preserving their relative order.
![image.png](https://assets.leetcode.com/users/images/dc5f20d6-7e29-45ce-9cf7-baa319fcc9fe_1740792433.414112.png)

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires applying a specific operation to an array and then moving all non-zero elements to the left while preserving their relative order. The initial thought is to perform the operations in-place and then shift the non-zero elements.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Iterate through the array from left to right (except the last element).
2. If two adjacent elements are equal, multiply the left one by 2 and set the right one to 0.
3. While iterating, move all non-zero elements to a new array, effectively shifting them to the left.
4. Handle the last element separately if it's non-zero.
5. Return the new array with non-zero elements shifted to the left and zeros at the end.

# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int[] applyOperations(int[] nums) {
        int n=nums.length;
        int[] arr=new int[n];        
        int i=0, j=0;

        // Apply operations and move non-zero elements
        for(i=0; i<n-1; i++){
            if(nums[i]==nums[i+1]){
                nums[i]=nums[i]*2;
                nums[i+1]=0;                
            }
            if(nums[i]!=0){
                arr[j++]=nums[i];
            }
        }

        // Handle the last element
        if(nums[i]!=0){
            arr[j++]=nums[i];
        }


        return arr;
    }
}
```
['Java']