Duplicate Zeros - Problem
Transform an array by duplicating zeros!

You're given a fixed-length integer array arr. Your task is to duplicate each occurrence of zero, shifting all remaining elements to the right. Here's the catch: the array size stays the same, so elements that get pushed beyond the original length are discarded.

Important: You must modify the input array in-place without returning anything.

Example: [1,0,2,3,0,4,5,0] becomes [1,0,0,2,3,0,0,4]
Notice how the original zeros at indices 1, 4, and 7 each get duplicated, pushing subsequent elements right until they fall off the end.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1,0,2,3,0,4,5,0]
โ€บ Output: [1,0,0,2,3,0,0,4]
๐Ÿ’ก Note: The zeros at indices 1, 4, and 7 are duplicated. The first zero creates [1,0,0,2,3,0,4,5], the second zero creates [1,0,0,2,3,0,0,4,5], and since we exceed the array length, the 5 is dropped. The final zero would create another duplicate but it falls outside the array bounds.
example_2.py โ€” Edge Case with Consecutive Zeros
$ Input: [1,0,0,1]
โ€บ Output: [1,0,0,0]
๐Ÿ’ก Note: The first zero at index 1 gets duplicated, shifting everything right: [1,0,0,0,1]. The second original zero would also get duplicated, but there's no space left in the array, so the final result is [1,0,0,0] with the trailing elements dropped.
example_3.py โ€” No Zeros
$ Input: [1,2,3,4]
โ€บ Output: [1,2,3,4]
๐Ÿ’ก Note: Since there are no zeros in the array, no duplication occurs and the array remains unchanged.

Constraints

  • 1 โ‰ค arr.length โ‰ค 104
  • 0 โ‰ค arr[i] โ‰ค 9
  • You must modify the array in-place
  • Do not return anything from your function

Visualization

Tap to expand
Factory Conveyor Belt - Duplicate ZerosConveyor Belt (Fixed Length)10230450Original items on the beltWorkerWorking backwards โ†’After Processing (Items Beyond Belt Fall Off)10023004Worker's Strategy1. Count zeros first (3 zeros found)2. Calculate final length: 8 + 3 = 113. Work backwards to avoid conflicts4. Duplicate each zero encountered5. Let excess items fall off naturallyโœ“ Efficient: Each item handled exactly once - O(n) time complexityโœ“ Safe: Working backwards prevents data loss during processing
Understanding the Visualization
1
Survey the Belt
First, count how many zeros exist to predict the final layout
2
Work Backwards
Start from the end and work backwards to avoid disturbing items you haven't processed yet
3
Duplicate Zeros
When you encounter a zero, place two zeros in the final positions
4
Handle Overflow
Items that would exceed the belt length naturally fall off
Key Takeaway
๐ŸŽฏ Key Insight: Working backwards with pre-calculated positions ensures optimal O(n) performance while safely handling in-place modifications without data corruption.
Asked in
Apple 35 Microsoft 28 Google 22 Amazon 18
38.5K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen