Duplicate Zeros (LeetCode 1089): Solution and Explanation

Easy
Array
Two Pointers

You get a fixed-length array. Write every zero twice, shifting the later elements right, and drop whatever is pushed past the end. The change happens in place.

Intuition

The obvious way is to scan left to right and, at each zero, shift the rest of the array one place right. That is correct but costs O(n) per zero, so it is O(n^2) on an array full of zeros.

Think about where each element ends up instead. If there are zeros zeros, the fully expanded array would be n + zeros long. An element's slot in that longer array is its index plus the number of zeros before it. The real array is just the first n slots of it.

Filling from the back is safe because the write position is never left of the read position. Every value you overwrite has already been read, so nothing is lost. Values whose slot is n or more are exactly the ones that fall off the end, so you simply skip writing them.

Approach: Count zeros, then two pointers from the back

  1. Count the zeros in arr.
  2. Set the read pointer i = n - 1 and the write pointer j = n + zeros - 1 on the imaginary longer array.
  3. While i >= 0, write arr[i] into slot j if j < n.
  4. If arr[i] is 0, move j left once more and write a second 0 there if j < n.
  5. Move both i and j one step left and repeat.

Step-by-step example

arr = [1,0,2,3,0,4,5,0]
  1. There are 3 zeros, so the expanded array would have 11 slots: i = 7 and j = 10.
  2. arr[7] = 0 belongs at slots 10 and 9. Both are past the end (n = 8), so both copies are dropped.
  3. arr[6] = 5 belongs at slot 8, also past the end, so it is dropped. arr[5] = 4 lands in slot 7.
  4. arr[4] = 0 lands in slots 6 and 5. arr[3] = 3 goes to slot 4 and arr[2] = 2 to slot 3.
  5. arr[1] = 0 lands in slots 2 and 1. Now j = i = 0, so arr[0] = 1 stays put. The result is [1,0,0,2,3,0,0,4].

Code: Python, Java, C++

1class Solution:
2    def duplicateZeros(self, arr: List[int]) -> None:
3        n = len(arr)
4        zeros = arr.count(0)
5        i, j = n - 1, n + zeros - 1
6        while i >= 0:
7            if j < n:
8                arr[j] = arr[i]
9            if arr[i] == 0:
10                j -= 1
11                if j < n:
12                    arr[j] = 0
13            i -= 1
14            j -= 1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass counts the zeros and one backward pass places every element, each with constant work.

Space

O(1)

Only the zero count and two pointers are stored; the array is rewritten in place.

Other approaches

Shift on every zero (time O(n^2), space O(1))

Scan left to right. When you see a zero, move every later element one slot right and write a second zero, then skip past it. It is the most direct translation of the statement but takes O(n) per zero.

Build a copy (time O(n), space O(n))

Build a new list by appending each value, and a second 0 after each zero, stopping at length n. Then copy it back into arr. It is simple but uses O(n) extra memory.

Common mistakes

  • Filling from the front. Writing a duplicated zero overwrites the next element before you have read it.
  • Starting j at n - 1. The write pointer must start at n + zeros - 1 so every element keeps its offset from the zeros before it.
  • Writing the second zero without checking j < n. The last zero that fits may have its copy pushed just past the end.
  • Returning a new array. The judge checks arr itself, so the function must modify it in place.

Frequently asked questions

Why is it safe to write from the back?

The write pointer j is always at or to the right of the read pointer i, because j - i equals the number of zeros still to the left. Every slot you overwrite has already been read.

What happens to a zero whose copy does not fit?

Its first copy lands in the last slot and its second copy would land past the end, so only one 0 is written. The check j < n before the second write handles this case.

Can the loop stop early?

Yes. Once j equals i, no zeros remain to the left, so every remaining element already sits in its final slot. Stopping there is an optimisation; continuing is harmless because each write puts a value back where it already is.