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 multiplynums[i]by2and setnums[i + 1]to0. 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 its0’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.
Examples
Example 1:
| |
Example 2:
| |
Constraints:
2 <= nums.length <= 20000 <= nums[i] <= 1000
Solution
Video explanation
Here is the video explaining below methods in detail. Please check it out:
Method 1 - Run the simulation with extra array
Here is the approach:
- Iterate Over The Array: Start iterating over
numsfrom index 0 ton - 2.- If
nums[i] == nums[i + 1], multiplynums[i]by2and setnums[i + 1]to0.
- If
- Shifting Zeros: After the above operations, shift all the zeros in the array to the end.
- This can be done by creating a new array where non-zero elements retain their relative order, followed by appending the zeros at the end.
Code
| |
| |
Complexity
- ⏰ Time complexity:
O(n)- Iteration over the array is
O(n)to apply operations. - Shifting zeros involves another
O(n)pass.
- Iteration over the array is
- 🧺 Space complexity:
O(n)3. The operations can be performed in-place (O(1)space) for the main operations. 4. However, shifting zeros explicitly uses a secondary array which requiresO(n)space.
Method 2 - Run the simulation in-place
Here is the approach:
- Apply Operations In-Place:
- Iterate over the array and perform the same operation (
nums[i] *= 2,nums[i + 1] = 0) directly in the array ifnums[i] == nums[i + 1].
- Iterate over the array and perform the same operation (
- Shift Non-Zero Elements In-Place:
- Use a pointer (
write) to track the position where non-zero elements should be placed. - Traverse the array and copy non-zero values to the
writeposition. - After all non-zero values are placed, fill the remaining positions with
0.
- Use a pointer (
Code
| |
| |
Complexity
- ⏰ Time complexity:
O(n)- Applying the operations:
O(n) - Shifting non-zero elements:
O(n)
- Applying the operations:
- 🧺 Space complexity:
O(1)
Method 3 - Run the simulation in-place one pass
Here is the approach:
- Apply Operations In-Place:
- Iterate over the array and perform the same operation (
nums[i] *= 2,nums[i + 1] = 0) directly in the array ifnums[i] == nums[i + 1].
- Iterate over the array and perform the same operation (
- Shift Non-Zero Elements In-Place:
- Use a pointer (
write) to track the position where non-zero elements should be placed. - Traverse the array and copy non-zero values to the
writeposition. - After all non-zero values are placed, fill the remaining positions with
0.
- Use a pointer (
Code
| |
| |
Complexity
- ⏰ Time complexity:
O(n). The entire process runs inO(n)asreaditerates from0ton - 1once. - 🧺 Space complexity:
O(1)