#283

Move Zeroes

Easy Read/write pointers

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Move all zeros to the end of the array in place, keeping the order of the non-zero elements.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: push zeros to the end, keep the other numbers in order.

Why this works: swap every non-zero to the next write position. Zeros drift right automatically.

Example: [0, 1, 0, 3, 12]

1 → swap with index 0 → [1, 0, 0, 3, 12]3 → swap with index 1 → [1, 3, 0, 0, 12]12 → swap with index 2 → [1, 3, 12, 0, 0]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Swapping zeros toward the end with bubble-like passes — O(n²).