 All Problems
Move Zeroes
easy
arrays
two pointers
facebook
amazon
microsoft
google

Given an integer array nums, move all 0s to the end while maintaining the relative order of non-zero elements. Do it in-place.

Example 1:

Input:  0 1 0 3 12
Output: 1 3 12 0 0

Example 2:

Input:  0
Output: 0

Constraints:

  • 1 ≤ nums.length ≤ 10⁴
  • -2³¹ ≤ nums[i] ≤ 2³¹ - 1

Input format: Space-separated integers.

Output format: Space-separated integers.

Run to check your code against the sample cases, or submit to run every case