✅ Two Pointers | Python | C++ | Java | C# | JS | Go | Swift | PHP | Rust | Kotlin | Dart | Ruby
IntuitionThe problem involves two distinct operations: first, we need to perform a series of comparisons and modifications on adjacent elements, and then we nee
# Intuition
**The problem involves two distinct operations:** first, we need to perform a series of comparisons and modifications on adjacent elements, and then we need to move all zeros to the end of the array. A natural way to approach this is to follow the instructions step by step.

# Approach
1. **Apply Operations:** Iterate through the array from index 0 to n-2 and apply the given rule:
    - If current element equals the next element, multiply the current element by 2 and set the next element to 0
    - Otherwise, skip and continue
2. **Shift Zeros:** After applying all operations, we need to move all zeros to the end while preserving the order of non-zero elements. We can do this in-place using a *two-pointer technique*:
    - Use a pointer to keep track of where the next non-zero element should go
    - Iterate through the array, moving non-zero elements to their correct positions
    - Fill the remaining positions with zeros

This approach follows the exact requirements of the problem and **manipulates the array in-place**, which is more efficient in terms of space.

# Complexity
- 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
```python3 []
class Solution:
    def applyOperations(self, nums: List[int]) -> List[int]:
        n = len(nums)
        
        # Apply operations
        for i in range(n - 1):
            if nums[i] == nums[i + 1]:
                nums[i] *= 2
                nums[i + 1] = 0
        
        # Shift zeros to the end (in-place)
        non_zero_idx = 0
        
        # Move all non-zero elements to the front
        for i in range(n):
            if nums[i] != 0:
                nums[non_zero_idx] = nums[i]
                non_zero_idx += 1
        
        # Fill the remaining positions with zeros
        for i in range(non_zero_idx, n):
            nums[i] = 0
        
        return nums
        
```
```cpp []
class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {
        int n = nums.size();
        
        // Apply operations
        for (int i = 0; i < n - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }
        
        // Shift zeros to the end (in-place)
        int nonZeroIdx = 0;
        
        // Move all non-zero elements to the front
        for (int i = 0; i < n; i++) {
            if (nums[i] != 0) {
                nums[nonZeroIdx++] = nums[i];
            }
        }
        
        // Fill the remaining positions with zeros
        while (nonZeroIdx < n) {
            nums[nonZeroIdx++] = 0;
        }
        
        return nums;
    }
};
```
```java []
class Solution {
    public int[] applyOperations(int[] nums) {
        int n = nums.length;
        
        // Apply operations
        for (int i = 0; i < n - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }
        
        // Shift zeros to the end (in-place)
        int nonZeroIdx = 0;
        
        // Move all non-zero elements to the front
        for (int i = 0; i < n; i++) {
            if (nums[i] != 0) {
                nums[nonZeroIdx++] = nums[i];
            }
        }
        
        // Fill the remaining positions with zeros
        while (nonZeroIdx < n) {
            nums[nonZeroIdx++] = 0;
        }
        
        return nums;
    }
}
```

```csharp []
class Solution {
    public int[] ApplyOperations(int[] nums) {
        int n = nums.Length;
        
        // Apply operations
        for (int i = 0; i < n - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }
        
        // Shift zeros to the end (in-place)
        int nonZeroIdx = 0;
        
        // Move all non-zero elements to the front
        for (int i = 0; i < n; i++) {
            if (nums[i] != 0) {
                nums[nonZeroIdx++] = nums[i];
            }
        }
        
        // Fill the remaining positions with zeros
        while (nonZeroIdx < n) {
            nums[nonZeroIdx++] = 0;
        }
        
        return nums;
    }
}
```
```javascript []
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var applyOperations = function(nums) {
    const n = nums.length;
    
    // Apply operations
    for (let i = 0; i < n - 1; i++) {
        if (nums[i] === nums[i + 1]) {
            nums[i] *= 2;
            nums[i + 1] = 0;
        }
    }
    
    // Shift zeros to the end (in-place)
    let nonZeroIdx = 0;
    
    // Move all non-zero elements to the front
    for (let i = 0; i < n; i++) {
        if (nums[i] !== 0) {
            nums[nonZeroIdx++] = nums[i];
        }
    }
    
    // Fill the remaining positions with zeros
    while (nonZeroIdx < n) {
        nums[nonZeroIdx++] = 0;
    }
    
    return nums;
};
```
```go []
func applyOperations(nums []int) []int {
    n := len(nums)
    
    // Apply operations
    for i := 0; i < n-1; i++ {
        if nums[i] == nums[i+1] {
            nums[i] *= 2
            nums[i+1] = 0
        }
    }
    
    // Shift zeros to the end (in-place)
    nonZeroIdx := 0
    
    // Move all non-zero elements to the front
    for i := 0; i < n; i++ {
        if nums[i] != 0 {
            nums[nonZeroIdx] = nums[i]
            nonZeroIdx++
        }
    }
    
    // Fill the remaining positions with zeros
    for i := nonZeroIdx; i < n; i++ {
        nums[i] = 0
    }
    
    return nums
}
```
```swift []
class Solution {
    func applyOperations(_ nums: [Int]) -> [Int] {
        var result = nums // Create a mutable copy
        let n = result.count
        
        // Apply operations
        for i in 0..<(n-1) {
            if result[i] == result[i+1] {
                result[i] *= 2
                result[i+1] = 0
            }
        }
        
        // Shift zeros to the end
        var nonZeroIdx = 0
        
        // Move all non-zero elements to the front
        for i in 0..<n {
            if result[i] != 0 {
                result[nonZeroIdx] = result[i]
                nonZeroIdx += 1
            }
        }
        
        // Fill the remaining positions with zeros
        for i in nonZeroIdx..<n {
            result[i] = 0
        }
        
        return result
    }
}
```
```php []
class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer[]
     */
    function applyOperations($nums) {
        $n = count($nums);
        
        // Apply operations
        for ($i = 0; $i < $n - 1; $i++) {
            if ($nums[$i] == $nums[$i + 1]) {
                $nums[$i] *= 2;
                $nums[$i + 1] = 0;
            }
        }
        
        // Shift zeros to the end (in-place)
        $nonZeroIdx = 0;
        
        // Move all non-zero elements to the front
        for ($i = 0; $i < $n; $i++) {
            if ($nums[$i] != 0) {
                $nums[$nonZeroIdx++] = $nums[$i];
            }
        }
        
        // Fill the remaining positions with zeros
        while ($nonZeroIdx < $n) {
            $nums[$nonZeroIdx++] = 0;
        }
        
        return $nums;
    }
}
```
```rust []
impl Solution {
    pub fn apply_operations(mut nums: Vec<i32>) -> Vec<i32> {
        let n = nums.len();
        
        // Apply operations
        for i in 0..n - 1 {
            if nums[i] == nums[i + 1] {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }
        
        // Shift zeros to the end (in-place)
        let mut non_zero_idx = 0;
        
        // Move all non-zero elements to the front
        for i in 0..n {
            if nums[i] != 0 {
                nums[non_zero_idx] = nums[i];
                non_zero_idx += 1;
            }
        }
        
        // Fill the remaining positions with zeros
        for i in non_zero_idx..n {
            nums[i] = 0;
        }
        
        nums
    }
}
```
```kotlin []
class Solution {
    fun applyOperations(nums: IntArray): IntArray {
        val n = nums.size
        
        // Apply operations
        for (i in 0 until n - 1) {
            if (nums[i] == nums[i + 1]) {
                nums[i] *= 2
                nums[i + 1] = 0
            }
        }
        
        // Shift zeros to the end (in-place)
        var nonZeroIdx = 0
        
        // Move all non-zero elements to the front
        for (i in nums.indices) {
            if (nums[i] != 0) {
                nums[nonZeroIdx] = nums[i]
                nonZeroIdx++
            }
        }
        
        // Fill the remaining positions with zeros
        for (i in nonZeroIdx until n) {
            nums[i] = 0
        }
        
        return nums
    }
}
```
```dart []
class Solution {
  List<int> applyOperations(List<int> nums) {
    int n = nums.length;
    
    // Apply operations
    for (int i = 0; i < n - 1; i++) {
      if (nums[i] == nums[i + 1]) {
        nums[i] *= 2;
        nums[i + 1] = 0;
      }
    }
    
    // Shift zeros to the end (in-place)
    int nonZeroIdx = 0;
    
    // Move all non-zero elements to the front
    for (int i = 0; i < n; i++) {
      if (nums[i] != 0) {
        nums[nonZeroIdx++] = nums[i];
      }
    }
    
    // Fill the remaining positions with zeros
    for (int i = nonZeroIdx; i < n; i++) {
      nums[i] = 0;
    }
    
    return nums;
  }
}
```
```ruby []
# @param {Integer[]} nums
# @return {Integer[]}
def apply_operations(nums)
  n = nums.length

  # Apply operations
  (0...n - 1).each do |i|
    if nums[i] == nums[i + 1]
      nums[i] *= 2
      nums[i + 1] = 0
    end
  end

  # Shift zeros to the end (in-place)
  non_zero_idx = 0

  # Move all non-zero elements to the front
  (0...n).each do |i|
    if nums[i] != 0
      nums[non_zero_idx] = nums[i]
      non_zero_idx += 1
    end
  end

  # Fill the remaining positions with zeros
  (non_zero_idx...n).each do |i|
    nums[i] = 0
  end

  nums
end
```

---

![upvote.png](https://assets.leetcode.com/users/images/affc2097-5d6a-4a59-97a7-6d7b25e5bc80_1740818993.4830308.png)



['Swift', 'Python', 'C++', 'Java', 'Go', 'Python3', 'Rust', 'Kotlin', 'JavaScript', 'C#']