Duplicate Zeros - Problem
Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.
Note: Elements 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.
Input & Output
Example 1 — Basic Duplication
$
Input:
arr = [1,0,2,3,0,4,5,0]
›
Output:
[1,0,0,2,3,0,0,4]
💡 Note:
Each zero is duplicated, shifting remaining elements right. Elements 5,0 are dropped as they exceed array length.
Example 2 — No Zeros
$
Input:
arr = [1,2,3]
›
Output:
[1,2,3]
💡 Note:
No zeros to duplicate, array remains unchanged.
Example 3 — All Zeros
$
Input:
arr = [0,0,0]
›
Output:
[0,0,0]
💡 Note:
First zero is duplicated, second zero pushes remaining elements out of bounds.
Constraints
- 1 ≤ arr.length ≤ 104
- 0 ≤ arr[i] ≤ 9
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Original array with zeros to duplicate
2
Duplicate Process
Each zero creates a copy, elements shift right
3
Final Result
Modified array with excess elements dropped
Key Takeaway
🎯 Key Insight: Work backwards to avoid overwriting elements before they're processed
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code