Apply a set of simple transformation rules to an array, then compact the result by moving zeros to the end.
You are given an integer array nums. Perform the following operation on the array from left to right:
nums[i] == nums[i + 1], replace nums[i] with 2 * nums[i] and replace nums[i + 1] with 0.After processing all adjacent pairs once, shift all zeros to the end of the array while preserving the relative order of the non-zero elements.
Return the transformed array.
0.nums of length n.The array is processed in-place conceptually, but return the final transformed array.
1 <= nExample 1
Input
nums = [1,2,2,1,1,0]
Output
[1,4,2,0,0,0]
Explanation
2 and 2 become 4 and 0.1 and 1 become 2 and 0.[1,4,2,0,0,0].Example 2
Input
nums = [0,0,3,3]
Output
[6,0,0,0]
Explanation
0 and 0 are equal, so the first becomes 0 and the second becomes 0.3 and 3 become 6 and 0.[6,0,0,0].Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.