Duplicate Zeros Problem

Duplicate Zeros Problem — ExecCode School DSA Practice

Solve the Duplicate Zeros problem on ExecCode. Free online school DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given a fixed-length integer array arr, duplicate every occurrence of zero in-place, shifting the remaining elements to the right, and drop any elements past the original array length so the size never changes.

Examples

Input arr = [1, 0, 2, 3, 0, 4, 5, 0]; Output [1, 0, 0, 2, 3, 0, 0, 4]. Input arr = [1, 2, 3]; Output [1, 2, 3]. Input arr = [0, 0, 0, 0, 0, 0]; Output [0, 0, 0, 0, 0, 0]

Constraints

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

Practice Duplicate Zeros free on ExecCode. Browse DSA problems, topic map, and placement guides.