/283. Move Zeroes

283. Move Zeroes

Easy
Arrays63.6% acceptance

Given an integer array arr, rearrange the elements in-place so that all zero values are moved to the end of the array, while preserving the relative order of the non-zero elements. Do not use extra space for another array.

Example 1

Input: [4,0,5,0,0,7]

Output: [4,5,7,0,0,0]

Explanation: Non-zero elements are moved forward, zeros are at the end.

Example 2

Input: [0,0,0,2]

Output: [2,0,0,0]

Explanation: Only one non-zero element, moved to front.

Example 3

Input: [1,2,3,4]

Output: [1,2,3,4]

Explanation: No zeros, array unchanged.

Constraints

  • 1 <= len(arr) <= 10_000
  • -231 <= arr[i] <= 231 - 1
  • Must operate in-place; no extra array copies
Python (current runtime)

Case 1

Input: [0,8,0,0,2,3]

Expected: [8,2,3,0,0,0]

Case 2

Input: [6,0,0,0,9,0]

Expected: [6,9,0,0,0,0]

Case 3

Input: [0,0,0,0,0]

Expected: [0,0,0,0,0]

Case 4

Input: [7]

Expected: [7]

Case 5

Input: [0]

Expected: [0]