🚀 Beats 100% | In-Place Processing + Zero Shifting | Apply Operations on Array
Youtube🚀 Beats 100% | In-Place Processing + Zero Shifting | Apply Operations on Array🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote💡 If this helpe
# Youtube
https://youtu.be/81UhXYJOz1g

![image.png](https://assets.leetcode.com/users/images/3494c257-469b-4399-a3bd-f8f6ea6ee477_1740799406.261016.png)


---

# 🚀 Beats 100% | In-Place Processing + Zero Shifting | Apply Operations on Array 

---

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

💡 **If this helped, don’t forget to upvote! 🚀🔥**  

---

## **💡 Problem Breakdown**  
Given an array `nums`, perform the following operations:  
1. If `nums[i] == nums[i+1]`, double `nums[i]` and set `nums[i+1] = 0`.  
2. Shift all nonzero elements to the left, keeping the order the same.  

### **Example:**  
#### **Input:**  
```plaintext
nums = [2,2,0,4,4,8]
```
#### **Output:**  
```plaintext
[4,8,8,0,0,0]
```
#### **Explanation:**  
- `2 + 2 = 4`, `nums[1] = 0`.  
- `4 + 4 = 8`, `nums[4] = 0`.  
- Shift nonzero elements left.  

---

## **🚀 Approach Overview**  

### 🔹 **Key Observations:**  
1. **Iterate through `nums`** and check adjacent elements.  
2. **If equal, double the value** and set the next to `0`.  
3. **Use a new array** to place nonzero elements sequentially.  

### 🔹 **Strategy:**  
1. Traverse `nums`, applying the doubling rule.  
2. Store the results in `newNums`, ensuring order is preserved.  
3. Return the modified array.  

---

## **⏳ Complexity Analysis**  
- **Time Complexity:** `O(N)`, since we traverse the array once.  
- **Space Complexity:** `O(N)`, for storing results.  

---

## **📝 Code Implementation**  

### **🔵 Java Solution**  
```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;
    }
}
```

---

### **🟢 C++ Solution**  
```cpp
class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {
        int n = nums.size();
        vector<int> newNums(n, 0); 
        int count = 0;

        for (int i = 0; i < n - 1; i++) {
            if (nums[i] != 0) {
                if (nums[i] == nums[i + 1]) {
                    newNums[count] = nums[i] * 2; 
                    nums[i + 1] = 0;
                    i++; 
                } else {
                    newNums[count] = nums[i]; 
                }
                count++;
            }
        }
        if (nums[n - 1] != 0) {
            newNums[count++] = nums[n - 1];
        }

        return newNums;
    }
};
```

---

### **🟣 Python3 Solution**  
```python
from typing import List

class Solution:
    def applyOperations(self, nums: List[int]) -> List[int]:
        new_nums = [0] * len(nums)
        count = 0
        i = 0

        while i < len(nums) - 1:
            if nums[i] != 0:
                if nums[i] == nums[i + 1]:
                    new_nums[count] = nums[i] * 2
                    i += 1  
                else:
                    new_nums[count] = nums[i]
                count += 1
            i += 1
        
        if i < len(nums) and nums[i] != 0:
            new_nums[count] = nums[i]

        return new_nums
```

---

### **🔴 C Solution**  
```c
int* applyOperations(int* nums, int numsSize, int* returnSize) {
    int* newNums = (int*)calloc(numsSize, sizeof(int)); 
    int count = 0;
    int i;

    for (i = 0; i < numsSize - 1; i++) {
        if (nums[i] != 0) {
            if (nums[i] == nums[i + 1]) {
                newNums[count] = nums[i] * 2;
                i++; 
            } else {
                newNums[count] = nums[i];
            }
            count++;
        }
    }

    if (i < numsSize && nums[i] != 0) {
        newNums[count] = nums[i];
    }

    *returnSize = numsSize;
    return newNums;
}
```

---

### **🟠 C# Solution**  
```csharp
using System;

public class Solution {
    public int[] ApplyOperations(int[] nums) {
        int n = nums.Length;
        int[] newNums = new int[n]; 
        int count = 0;

        for (int i = 0; i < n - 1; i++) {
            if (nums[i] != 0) {
                if (nums[i] == nums[i + 1]) {
                    newNums[count] = nums[i] * 2; 
                    nums[i + 1] = 0; 
                    i++; 
                } else {
                    newNums[count] = nums[i]; 
                }
                count++; 
            }
        }
        if (nums[n - 1] != 0) {
            newNums[count++] = nums[n - 1];
        }

        return newNums;
    }
}
```

---

### **🟡 JavaScript Solution**  
```js
var applyOperations = function(nums) {
    let n = nums.length;
    let newNums = new Array(n).fill(0); 
    let count = 0;

    for (let i = 0; i < n - 1; i++) {
        if (nums[i] !== 0) {
            if (nums[i] === nums[i + 1]) {
                newNums[count] = nums[i] * 2;
                nums[i + 1] = 0; 
                i++; 
            } else {
                newNums[count] = nums[i]; 
            }
            count++; 
        }
    }

    if (nums[n - 1] !== 0) {
        newNums[count++] = nums[n - 1];
    }

    return newNums;
};
```

---

### **🟣 TypeScript Solution**  
```ts
function applyOperations(nums: number[]): number[] {
    let n = nums.length;
    let newNums: number[] = new Array(n).fill(0); 
    let count = 0;

    for (let i = 0; i < n - 1; i++) {
        if (nums[i] !== 0) {
            if (nums[i] === nums[i + 1]) {
                newNums[count] = nums[i] * 2;
                nums[i + 1] = 0;
                i++; 
            } else {
                newNums[count] = nums[i]; 
            }
            count++; 
        }
    }

    if (nums[n - 1] !== 0) {
        newNums[count++] = nums[n - 1];
    }

    return newNums;
}
```

---

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

💡 **If this helped, don’t forget to upvote! 🚀🔥**  

['Array', 'C', 'C++', 'Java', 'TypeScript', 'Python3', 'JavaScript', 'C#']