Duplicate every zero in an array in place while keeping the array length fixed.
Given a fixed-length integer array, duplicate each occurrence of 0 by shifting the remaining elements to the right. Elements that fall off the end of the array are discarded.
You must modify the array in place. The final array must keep the same length as the input.
Return the array after all zeros have been duplicated and all other elements shifted accordingly.
arr of fixed length.0 and shifting elements right.Example 1
Input
arr = [1,0,2,3,0,4,5,0]
Output
[1,0,0,2,3,0,0,4]
Explanation
Each zero is duplicated, and elements that would move past the end are dropped.
Example 2
Input
arr = [1,2,3]
Output
[1,2,3]
Explanation
There are no zeros to duplicate.
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.