Duplicate Zeros

Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that 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.

Example 1
Inputarr = [1,0,2,3,0,4,5,0]
Output[1,0,0,2,3,0,0,4]
After calling your function, the input array is modified to [1, 0, 0, 2, 3, 0, 0, 4].
Example 2
Inputarr = [1,2,3]
Output[1,2,3]
After calling your function, the input array is modified to [1, 2, 3].

Constraints

  • 1 <= arr.length <= 10^4
  • 0 <= arr[i] <= 9

Asked at 4 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate