21. Duplicate Zeros

Given a fixed-length integer array arr. Duplicate each occurrence of zero, shifting the remaining elements to the right.
Note:
  • Elements beyond the original array length are discarded.
  • Modifications must be done in place on the input array.
  • Do not return anything.
Example 1
Input: arr = [1,2,3]
Output: [1,2,3]
Explanation: There are no zeros in the array — the array remains unchanged.
Example 2
Input: arr = [0,0,0]
Output: [0,0,0]
Explanation: If each zero were duplicated: [0,0,0] -> [0,0,0,0,0,0], but the array is limited to the original length -> result [0,0,0].
Example 3
Input: arr = [1,0,2,3,0]
Output: [1,0,0,2,3]
Explanation: Duplicate the zero at position 1: [1,0,2,3,0] -> [1,0,0,2,3] (elements on the right are shifted).
arraysmedium
JavaScript
Loading...
Line 1, Char 1