Skip to content

Practice Apply Operations to an Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2460
LeetCode Problem

Apply Operations to an Array

Problem Statement

Visit indices i from 0 through nums.length - 2 in order. If nums[i] equals nums[i + 1], double nums[i] and set nums[i + 1] to zero; otherwise do nothing. Later steps see earlier changes. Finally move all zeros to the end while retaining the order of nonzero values, and return the array.

Source: Apply Operations to an Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,2,1,1,0]
Output
[1,4,2,0,0,0]

# Example 2

Input
nums = [0,1]
Output
[1,0]

Constraints

2 <= nums.length <= 2000
0 <= nums[i] <= 1000

Related LeetCode Problems

Practice more problems similar to Apply Operations to an Array.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.