Loading…
Loading…
Given an array, move all 0s to the end while keeping the relative order of the non-zero elements. Print the resulting array, space-separated.
Input: 0 1 0 3 12
Output: 1 3 12 0 0
A "write" pointer tracks where the next non-zero element should go. Scan once, moving every non-zero element to the write position, then fill the remainder with zeroes.
Input (stdin)
Output
Input (stdin)
Output
Sign in to track solved problems and earn XP.