Move zeros
Input
nums = [0, 1, 0, 3, 12]
Output
[1, 3, 12, 0, 0]
All non-zero elements keep their relative order and zeros are moved to the end.
Full lesson preview
Reorder an array so that all zeros appear at the end while keeping the relative order of the non-zero elements.
Problem statement
Task
Examples
Input
nums = [0, 1, 0, 3, 12]
Output
[1, 3, 12, 0, 0]
All non-zero elements keep their relative order and zeros are moved to the end.
Input format
Output format
Constraints
Samples
Input
nums = [1, 2, 3]
Output
[1, 2, 3]
No zeros to move; output equals input.