🌟 Beats 96.37% 🌟 | ✔️ Easiest Solution & Beginner Friendly ✔️ | 🔥 1MS & 2MS 🔥 | 2 Solutions 
🚀 Solution 1 : Brute Force (2MS)💡 IntuitionThe problem requires modifying an array based on adjacent equal elements and then shifting all zeros to the end. The

---
![image.png](https://assets.leetcode.com/users/images/178f70a1-8065-4b5a-9a89-c7c2a28b4cdb_1740800826.0790913.png)

---

![cat.jpeg](https://assets.leetcode.com/users/images/65bf8eab-255c-4930-8364-d90ee6f70af7_1740802544.738743.jpeg)

---
# 🚀 Solution 1 : Brute Force (2MS)
---

# 💡 Intuition
The problem requires modifying an `array` based on **adjacent equal elements** and then shifting all `zeros` to the `end`. The approach involves **two main steps**: first, iterating through the `array` and **merging adjacent equal elements** while replacing the next element with `zero`; second, **rearranging the array** by moving non-zero elements **forward** and shifting all `zeros` to the end.

# 📜🔢  Approach
`Step 1` **Merge Adjacent Elements** 
- Iterate through the `array` and whenever two adjacent elements are **equal**, double the **first** one and set the **second** one to `zero`.

`Step 2` **Count Zeroes**
- Traverse the `array` again to `count` the number of `zero` elements.

`Step 3` **Extract Non-Zero Elements**
- Store all **non-zero** elements in a separate `list`.

`Step 4` **Rearrange the Array**
- Overwrite the original `array` with the **non-zero** elements followed by the counted `zeroes`.

`Step 5` **Return the Modified Array.**

# ⏳📊 Complexity
- Time complexity : $$O(n)$$
- Space complexity : $$O(n)$$

# 💻 Code
```Java []
class Solution 
{
    public int[] applyOperations(int[] nums) 
    {
        // Step 1: Merge adjacent elements if they are equal
        for(int i = 0; i < nums.length - 1; i++) 
        {
            if(nums[i] == nums[i + 1]) 
            {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }

        // Step 2: Count zeroes
        int countZero = 0;
        for(int num : nums) 
        {
            if(num == 0) 
            {
                countZero++;
            }
        }

        // Step 3: Extract non-zero elements
        ArrayList<Integer> list = new ArrayList<>();
        for(int num : nums) 
        {
            if(num > 0) 
            {
                list.add(num);
            }
        }

        // Step 4: Rearrange the array by moving non-zero elements first
        for(int i = 0; i < list.size(); i++) 
        {
            nums[i] = list.get(i);
        }

        // Step 5: Append zeros at the end
        for(int i = list.size(); i < nums.length; i++) 
        {
            nums[i] = 0;
        }

        // Step 6: Return the modified array
        return nums;
    }
}
```
``` C++ []
class Solution 
{
public:
    vector<int> applyOperations(vector<int>& nums) 
    {
        // Step 1: Merge adjacent elements if they are equal
        for(int i = 0; i < nums.size() - 1; i++) 
        {
            if(nums[i] == nums[i + 1]) 
            {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }

        // Step 2: Count zeroes and extract non-zero elements
        vector<int> result;
        int zeroCount = 0;
        for(int num : nums) 
        {
            if(num == 0) 
            {
                zeroCount++;
            }
            else 
            {
                result.push_back(num);
            }
        }

        // Step 3: Rearrange array by placing non-zero elements first
        for(int i = 0; i < result.size(); i++) 
        {
            nums[i] = result[i];
        }

        // Step 4: Append zeroes at the end
        for(int i = result.size(); i < nums.size(); i++) 
        {
            nums[i] = 0;
        }

        // Step 5: Return the modified array
        return nums;
    }
};
```
---
# 🚀 Solution 2 : Optimized (1MS)
---

# 💡 Intuition
The problem requires us to modify an `array` by merging **adjacent equal elements** and then shifting all **non-zero elements** forward while maintaining the **relative order**. Here, we use an extra `array` `result[]` to store the final values, ensuring all non-zero elements appear `first`, followed by `zeroes`. This simplifies the **shifting process** and avoids modifying the **input array** in place.

# 📜🔢 Approach
`Step 1` **Merge Adjacent Elements**
- Iterate through the `array` and whenever **two adjacent elements** are `equal`, double the **first** one and set the **second** one to `zero`.

`Step 2` **Create a Result Array**
- Initialize a new array `result[]` of the same **size** as `nums[]`.

`Step 3` **Copy Non-Zero Elements**
- Traverse `nums[]`, copy all **non-zero** elements into `result[]` sequentially.

`Step 4` **Return the result[] array**

# ⏳📊 Complexity
- Time complexity : $$O(n)$$
- Space complexity : $$O(n)$$

# 💻 Code
```Java []
class Solution 
{
    public int[] applyOperations(int[] nums) 
    {
        // Step 1: Create a result array initialized with zeros
        int[] result = new int[nums.length]; 
        
        // Step 2: Merge adjacent elements if they are equal
        for(int i = 0; i < nums.length - 1; i++) 
        {
            if(nums[i] == nums[i + 1]) 
            {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }    

        // Step 3: Copy non-zero elements into result array
        int j = 0;
        for(int i = 0; i < nums.length; i++) 
        {
            if(nums[i] != 0) 
            {
                result[j] = nums[i];
                j++;
            }
        }

        // Step 4: Return the modified array
        return result;
    }
}
```
``` C++ []
class Solution 
{
public:
    vector<int> applyOperations(vector<int>& nums) 
    {
        // Step 1: Create a result array initialized with zeros
        int n = nums.size();
        vector<int> result(n, 0); 

        // Step 2: Merge adjacent elements if they are equal
        for(int i = 0; i < n - 1; i++) 
        {
            if(nums[i] == nums[i + 1]) 
            {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }

        // Step 3: Copy non-zero elements into result array
        int j = 0;
        for(int i = 0; i < n; i++) 
        {
            if(nums[i] != 0) 
            {
                result[j] = nums[i];
                j++;
            }
        }

        // Step 4: Return the modified array
        return result;
    }
};
```
['Array', 'Two Pointers', 'Greedy', 'Suffix Array', 'Sorting', 'Heap (Priority Queue)', 'Simulation', 'Combinatorics', 'C++', 'Java']