"Applying Operations and Rearranging an Array of Non-Negative Integers"
IntuitionThe problem requires us to sequentially apply operations on an array where we double the value of an element if it is equal to its neighbor and set the
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to sequentially apply operations on an array where we double the value of an element if it is equal to its neighbor and set the neighbor to zero. After processing all elements, we also need to shift all zeros to the end of the array. The key idea is to iterate through the array, apply the operations where applicable, and then handle the zeros at the end.
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Iterate through the array**: Use a loop to go through each element of the array, comparing each element with the next one.
2. **Apply operations**: If two adjacent elements are equal, double the first one and set the second to zero. If they are not equal, just move the current element to a new position in the result.
3. **Shift zeros**: After processing the array, fill the rest of the array with zeros to move them to the end.
4. **Return the modified array**.
# Complexity
- **Time complexity**:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
The time complexity is O(n),where n is the length of the input array. This is because we go through the array a constant number of times.
- **Space complexity**:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
The space complexity is O(1) since we are modifying the array in place and not using any additional data structures that scale with input size.
# Code
```java []
class Solution {
    public int[] applyOperations(int[] nums) {
        int n = nums.length;
        int idx = 0,i=0;
        while(i<n-1){
            if(nums[i]!=0){
                if(nums[i]==nums[i+1]){
                    nums[idx++]=nums[i]*2;
                    i++;
                }
                else nums[idx++]=nums[i];
            }
            i++;
        }
        if(i<=n-1 && nums[n-1]!=0)nums[idx++]=nums[n-1];
        while(idx<n){
            nums[idx++]=0;
        }
        return nums;
    }
}
```
['Java']