Beats 100% with JAVA Beginners Friendly || YoU HaVe To ChEck ThiS OUt
IntuitionThe main idea behind solving this problem is that if we follow the same process described in the problem's description, we need to first modify the arr
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The main idea behind solving this problem is that if we follow the same process described in the problem's description, we need to first modify the array as instructed and then move all the `0`s to the last positions. This is the naive brute-force approach I initially thought of, but it takes a time complexity of O(2n) in the worst case. To optimize the time complexity, I decided to go with a more beginner-friendly approach by creating a new array called `result`.

![7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg](https://assets.leetcode.com/users/images/81732cac-737b-4df2-8dcc-153902117ce0_1740879261.37014.jpeg)


# Approach
<!-- Describe your approach to solving the problem. -->
After reviewing some solutions from the **LeetCode discussions**, I found that certain test cases were particularly useful in guiding me toward solving this problem. Here are the test cases that helped me understand the issue and improve my approach by 98%:

- Test Case 1: `[0, 1, 0, 1]`
- Test Case 2: `[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]`
- Test Case 3: `[1, 3, 2, 3, 2, 2, 1, 2, 2, 1]`
- Test Case 4: `[6, 4, 1, 18, 1, 4, 14, 9, 7, 17, 18, 14, 8, 12, 20, 16, 7, 5, 9, 3, 3, 17, 2, 12, 8, 9, 8, 12, 19, 14, 7, 19, 19, 8, 12, 20, 9, 7, 3, 0, 13, 2, 7, 16, 14, 19, 18, 7, 4, 8, 4, 14, 15, 9, 4, 12, 15, 12, 1, 10, 3, 3, 16, 17, 8, 4, 2, 19, 7, 19, 18, 20, 5, 20, 16, 12, 16, 10, 16, 15, 15, 4, 11, 4, 3, 3, 5, 13, 13, 15, 13, 11, 9, 9, 7, 20, 12, 0, 16, 1]`

By analyzing these test cases, I refined my approach to solving the problem with greater accuracy and efficiency.

Use the following images for best understanding : 
![IMG_20250302_050824.jpg](https://assets.leetcode.com/users/images/c7753cdd-923a-412b-ae48-6e458aab3c01_1740872878.2119477.jpeg)

# 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[] result = new int[nums.length];
        int count = 0;
        int i;
        for(i = 0; i < nums.length - 1; i++){
            if(nums[i] != 0){
                if(nums[i] == nums[i + 1]){
                    result[count] = nums[i] * 2;
                    i++;
                }
                else{
                    result[count] = nums[i];
                }
                count++;
            }
        }
        if(i != nums.length){
            result[count] = nums[nums.length - 1];
        }
        return result;
    }
}
```
['Java']