Apply Operations to an Array (LeetCode 2460): Solution and Explanation
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
- For i from 0 to n - 2: if nums[i] == nums[i + 1], set nums[i] *= 2 and nums[i + 1] = 0.
- Set write = 0.
- For read from 0 to n - 1: if nums[read] != 0, swap nums[read] and nums[write], then add 1 to write.
- Return nums.
Step-by-step example
nums = [1,2,2,1,1,0]
- i = 0: 1 and 2 differ, skip.
- i = 1: 2 and 2 match, so nums becomes [1,4,0,1,1,0].
- 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.
- Compaction: 1 stays at 0, 4 stays at 1, the 0 at index 2 is skipped, 2 swaps into index 2.
- 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 nums1class Solution {
2 public int[] applyOperations(int[] nums) {
3 int n = nums.length;
4 for (int i = 0; i + 1 < n; i++) {
5 if (nums[i] == nums[i + 1]) {
6 nums[i] *= 2;
7 nums[i + 1] = 0;
8 }
9 }
10 int write = 0;
11 for (int read = 0; read < n; read++) {
12 if (nums[read] != 0) {
13 int t = nums[read];
14 nums[read] = nums[write];
15 nums[write++] = t;
16 }
17 }
18 return nums;
19 }
20}1class Solution {
2public:
3 vector<int> applyOperations(vector<int>& nums) {
4 int n = nums.size();
5 for (int i = 0; i + 1 < n; i++) {
6 if (nums[i] == nums[i + 1]) {
7 nums[i] *= 2;
8 nums[i + 1] = 0;
9 }
10 }
11 int write = 0;
12 for (int read = 0; read < n; read++) {
13 if (nums[read] != 0) {
14 swap(nums[read], nums[write++]);
15 }
16 }
17 return nums;
18 }
19};Time and space complexity
O(n)
One pass for the merges and one pass for the compaction.
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.