Apply Operations to an Array (LeetCode 2460): Solution and Explanation

Easy
Array
Two Pointers
Simulation

Merge each pair of equal neighbors in order (left doubles, right becomes 0), then move all zeros to the end while keeping the other values in order.

Intuition

The operations are sequential, so a merge at position i can turn nums[i + 1] into 0 before operation i + 1 looks at it. A single left-to-right pass that edits the array in place models this exactly.

The second part is Move Zeroes. A write pointer marks where the next nonzero value belongs, and a read pointer scans everything.

Every nonzero value read is swapped down to write, so nonzeros keep their order and zeros drift to the tail.

Approach: Simulate, then two-pointer compaction

  1. For i from 0 to n - 2: if nums[i] == nums[i + 1], set nums[i] *= 2 and nums[i + 1] = 0.
  2. Set write = 0.
  3. For read from 0 to n - 1: if nums[read] != 0, swap nums[read] and nums[write], then add 1 to write.
  4. Return nums.

Step-by-step example

nums = [1,2,2,1,1,0]
  1. i = 0: 1 and 2 differ, skip.
  2. i = 1: 2 and 2 match, so nums becomes [1,4,0,1,1,0].
  3. i = 2: 0 and 1 differ. i = 3: 1 and 1 match, so nums becomes [1,4,0,2,0,0]. i = 4: 0 and 0 match, still 0.
  4. Compaction: 1 stays at 0, 4 stays at 1, the 0 at index 2 is skipped, 2 swaps into index 2.
  5. Result [1,4,2,0,0,0].

Code: Python, Java, C++

1class Solution:
2    def applyOperations(self, nums: List[int]) -> List[int]:
3        n = len(nums)
4        for i in range(n - 1):
5            if nums[i] == nums[i + 1]:
6                nums[i] *= 2
7                nums[i + 1] = 0
8        write = 0
9        for read in range(n):
10            if nums[read] != 0:
11                nums[write], nums[read] = nums[read], nums[write]
12                write += 1
13        return nums
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass for the merges and one pass for the compaction.

Space

O(1)

Both passes edit nums in place with a few index variables.

Other approaches

Build a new array (time O(n), space O(n))

After the merge pass, copy the nonzero values into a new array and pad it with zeros. Simpler to read, but uses O(n) extra space.

Overwrite then fill (time O(n), space O(1))

Instead of swapping, write each nonzero value to nums[write] and fill the rest with zeros at the end. Same complexity with fewer writes per value.

Common mistakes

  • Applying the merges to a copy of the original array, which misses how one merge feeds the next comparison.
  • Shifting zeros before merging; the problem merges first.
  • Sorting to move zeros, which destroys the order of the nonzero values.

Frequently asked questions

Why can't I check all pairs at once?

Because each operation sees the earlier ones. In [2,2,2] the first merge gives [4,0,2], so the second pair is 0 and 2 and does not merge.

Does merging two zeros matter?

It runs, but 0 * 2 is still 0, so the array does not change.

Can a doubled value merge again with the next value?

No. After nums[i] doubles, nums[i + 1] becomes 0 and the next operation compares that 0 with nums[i + 2], not the doubled value.