##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2460.html

# 2460. Apply Operations to an Array

• Difficulty: Easy.
• Related Topics: Array, Simulation.
• Similar Questions: Remove Duplicates from Sorted Array, Move Zeroes.

## Problem

You are given a 0-indexed array nums of size n consisting of non-negative integers.

You need to apply n - 1 operations to this array where, in the ith operation (0-indexed), you will apply the following on the ith element of nums:

• If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0. Otherwise, you skip this operation.

After performing all the operations, shift all the 0’s to the end of the array.

• For example, the array [1,0,2,0,0,1] after shifting all its 0’s to the end, is [1,2,1,0,0,0].

Return the resulting array.

Note that the operations are applied sequentially, not all at once.

Example 1:

Input: nums = [1,2,2,1,1,0]
Output: [1,4,2,0,0,0]
Explanation: We do the following operations:
- i = 0: nums and nums are not equal, so we skip this operation.
- i = 1: nums and nums are equal, we multiply nums by 2 and change nums to 0. The array becomes [1,4,0,1,1,0].
- i = 2: nums and nums are not equal, so we skip this operation.
- i = 3: nums and nums are equal, we multiply nums by 2 and change nums to 0. The array becomes [1,4,0,2,0,0].
- i = 4: nums and nums are equal, we multiply nums by 2 and change nums to 0. The array becomes [1,4,0,2,0,0].
After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].


Example 2:

Input: nums = [0,1]
Output: [1,0]
Explanation: No operation can be applied, we just shift the 0 to the end.


Constraints:

• 2 <= nums.length <= 2000

• 0 <= nums[i] <= 1000

## Solution (Java, C++, Python)

• class Solution {
public int[] applyOperations(int[] nums) {
int n = nums.length;
for (int i = 0; i < n - 1; ++i) {
if (nums[i] == nums[i + 1]) {
nums[i] <<= 1;
nums[i + 1] = 0;
}
}
int[] ans = new int[n];
int j = 0;
for (int i = 0; i < n; ++i) {
if (nums[i] != 0) {
ans[j++] = nums[i];
}
}
for (int i = 0; i < n; ++i) {
if (nums[i] == 0) {
ans[j++] = nums[i];
}
}
return ans;
}
}

• class Solution {
public:
vector<int> applyOperations(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n - 1; ++i) {
if (nums[i] == nums[i + 1]) {
nums[i] <<= 1;
nums[i + 1] = 0;
}
}
vector<int> ans(n);
int j = 0;
for (int i = 0; i < n; ++i) if (nums[i]) ans[j++] = nums[i];
for (int i = 0; i < n; ++i) if (!nums[i]) ans[j++] = nums[i];
return ans;
}
};

• class Solution:
def applyOperations(self, nums: List[int]) -> List[int]:
n = len(nums)
for i in range(n - 1):
if nums[i] == nums[i + 1]:
nums[i] <<= 1
nums[i + 1] = 0
ans = [v for v in nums if v]
ans +=  * (n - len(ans))
return ans


• func applyOperations(nums []int) (ans []int) {
n := len(nums)
for i := 0; i < n - 1; i++ {
if nums[i] == nums[i + 1] {
nums[i] <<= 1
nums[i + 1] = 0
}
}
for _, v := range nums {
if v != 0 {
ans = append(ans, v)
}
}
for _, v := range nums {
if v == 0 {
ans = append(ans, v)
}
}
return
}


Explain:

nope.

Complexity:

• Time complexity : O(n).
• Space complexity : O(n).