7. Move Zeros to End

EasyArrayArray

Given an integer array nums, move all zeros to the end while maintaining the relative order of all non-zero elements.

You must do this in-place without making a copy of the array.

Input: An integer array nums of length n.

Output: The same array with zeros at the end and non-zeros in original relative order.

Examples

Example 1
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Explanation: Non-zeros in order: [1,3,12]. Zeros fill the tail. Result: [1,3,12,0,0].
Example 2
Input: [0,0,1]
Output: [1,0,0]
Explanation: Non-zero: [1]. Two zeros go to end. Result: [1,0,0].
Example 3
Input: [1,2,3]
Output: [1,2,3]
Explanation: No zeros present. Array unchanged.

Constraints

Asked by

CapgeminiAccentureCognizantInfosysBloombergMeta
Solve this problem in the editor →