✅2 Method's ||🌟JAVA||🧑‍💻 BEGINNER FREINDLY||C++||Python3
Approach 1: Two-Pass1.Apply Operations and Shift Zeros in a Single Pass:
.  Use a pointer index to track the position where the next non-zero element should be


# Approach 1: Two-Pass
<!-- Describe your approach to solving the problem. -->
**1.Apply Operations and Shift Zeros in a Single Pass:**
.  Use a pointer ```index``` to track the position where the next non-zero element should be placed.

.Iterate through the array:
   .If the current element (```nums[i]```) is equal to the next element (```nums[i + 1]```):
   .Double the value of``` nums[i]```.
Set ```nums[i + 1]``` to 0.
   .If the current element ```(nums[i])``` is not zero, move it to ```nums[index] ```and increment index.

   .After processing all elements, fill the remaining positions from ```index``` to ```nums.length - 1``` with zeros.

**2.Return the Modified Array:**
.The array is modified in place, so return ```nums``` as the result.

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

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

### Step-by-Step Table

### nums = [1, 2, 2, 1, 1, 0]
![image.png](https://assets.leetcode.com/users/images/482fea11-6b7c-44e7-b5d3-c462f82de549_1740787922.294513.png)


# 💻Code Implementation
```java []
class Solution {
    public int[] applyOperations(int[] nums) {
        
           for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                nums[i] *= 2; 
                nums[i + 1] = 0; 
            }
        }

        
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[index++] = nums[i]; 
            }
        }

        
        while (index < nums.length) {
            nums[index++] = 0;
        }

        return nums;
        
    }
}
```
```c++ []

#include <vector>
using namespace std;

class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {
        // Step 1: Merge adjacent equal elements
        for (int i = 0; i < nums.size() - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                nums[i] *= 2; 
                nums[i + 1] = 0; 
            }
        }

        // Step 2: Shift non-zero elements to the front
        int index = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] != 0) {
                nums[index++] = nums[i]; 
            }
        }

        // Step 3: Fill the rest of the array with zeros
        while (index < nums.size()) {
            nums[index++] = 0;
        }

        return nums;
    }
};
```
```python []
class Solution:
    def applyOperations(self, nums):
        # Step 1: Merge adjacent equal elements
        for i in range(len(nums) - 1):
            if nums[i] == nums[i + 1]:
                nums[i] *= 2
                nums[i + 1] = 0

        # Step 2: Shift non-zero elements to the front
        index = 0
        for i in range(len(nums)):
            if nums[i] != 0:
                nums[index] = nums[i]
                index += 1

        # Step 3: Fill the rest of the array with zeros
        while index < len(nums):
            nums[index] = 0
            index += 1

        return nums
```
___

# Approach 2: Two-Pointer

**1.Merging and Shifting:**
   .We iterate through the array with a single loop.
   .If two adjacent elements are equal, we double the first one and place it at the current ```index```, then increment ```i``` to skip the next element since it has been merged.
   .If the current element is non-zero and not part of a merge, we place it at the current ```index```.

**2.Filling Zeros:**
After processing all elements, we fill the remaining positions in the array with zeros.

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

- Space complexity:$$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# 💻Code Implementation
```java []
class Solution {
    public int[] applyOperations(int[] nums) {
        int n = nums.length;

        // Step 1: Apply the operations and shift non-zero elements to the front
        int index = 0; // Pointer to track the position for non-zero elements

        for (int i = 0; i < n; i++) {
            if (i < n - 1 && nums[i] == nums[i + 1]) {
                nums[i] *= 2; // Double the current element
                nums[i + 1] = 0; // Set the next element to 0
            }

            // Shift non-zero elements to the front
            if (nums[i] != 0) {
                nums[index] = nums[i]; // Place non-zero element at the current index
                if (index != i) {
                    nums[i] = 0; // Set the original position to 0 if it was moved
                }
                index++; // Move the index forward
            }
        }

        return nums;
    }
}
```
```c++ []
class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {
        int n = nums.size();
        int index = 0; // Pointer to track the position for non-zero elements

        for (int i = 0; i < n; i++) {
            if (i < n - 1 && nums[i] == nums[i + 1]) {
                nums[i] *= 2; // Double the current element
                nums[i + 1] = 0; // Set the next element to 0
            }

            // Shift non-zero elements to the front
            if (nums[i] != 0) {
                nums[index] = nums[i]; // Place non-zero element at the current index
                if (index != i) {
                    nums[i] = 0; // Set the original position to 0 if it was moved
                }
                index++; // Move the index forward
            }
        }

        // Fill the remaining positions with zeros
        while (index < n) {
            nums[index++] = 0;
        }

        return nums;
    }
};
```
```python []
class Solution:
    def applyOperations(self, nums):
        n = len(nums)
        index = 0  # Pointer to track the position for non-zero elements

        for i in range(n):
            if i < n - 1 and nums[i] == nums[i + 1]:
                nums[i] *= 2  # Double the current element
                nums[i + 1] = 0  # Set the next element to 0

            # Shift non-zero elements to the front
            if nums[i] != 0:
                nums[index] = nums[i]  # Place non-zero element at the current index
                if index != i:
                    nums[i] = 0  # Set the original position to 0 if it was moved
                index += 1  # Move the index forward

        # Fill the remaining positions with zeros
        while index < n:
            nums[index] = 0
            index += 1

        return nums
```
**If you found my solution helpful, I would greatly appreciate your upvote, as it would motivate me to continue sharing more solutions.**

🔼 **Please Upvote**
🔼 **Please Upvote**
🔼 **Please Upvote**

['Array', 'Two Pointers', 'Python', 'C++', 'Java', 'TypeScript', 'Python3', 'JavaScript']