Skip to main content
CodeOath
← All problems

Problem

Move Zeroes

Easy
  • arrays
  • two-pointers

Return nums with all of its zeros pushed to the back. The other values stay in the order they were in.

Example 1
Input
nums = [0, 4, 0, 0, 7, 2]
Output
[4, 7, 2, 0, 0, 0]
Explanation

4, 7 and 2 keep their order and move to the front. The three zeros go to the back.

Example 2
Input
nums = [1, 2, 3]
Output
[1, 2, 3]
Explanation

there is no zero to move, so the array comes back the same.

Constraints:

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Follow-up: can you do it in one pass by moving values inside nums, without building a second array?

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.