Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right. Elements pushed beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.
Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right. Elements pushed beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.
Example 1:
Input: {"arr":[1,0,2,3,0,4,5,0]}
Output: [1,0,0,2,3,0,0,4]
Constraints:
1≤arr.length≤104
0≤arr[i]≤9
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"arr":[1,0,2,3,0,4,5,0]}
Count the zeros: there are 3. With every zero doubled, the full result would be 11 long, but only the first 8 slots fit.
original
1
0
2
3
0
4
5
0
i ▼
1
0
2
3
0
4
5
0
0
1
2
3
4
5
6
7
8
9
10
Dashed slots 8..10 exist only on the imaginary longer array; anything written there is dropped.