Move Zeroes

easy

Move all zeroes to end while maintaining order of non-zero elements

Move Zeroes

Key Insight

Slow marks position for non-zero, swap when fast finds non-zero

Step 1Setup
slow
fast
0
0
1
1
0
2
3
3
12
4
same direction

Both pointers start at 0. Fast scans for non-zeros.

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Move Zeroes

Slow marks position for non-zero, swap when fast finds non-zero

  1. Setup

    Both pointers start at 0. Fast scans for non-zeros.

  2. Found Non-Zero

    fast=1 has value 1. Swap with slow position.

  3. Continue

    fast=3 has value 3. Swap with slow=1.

  4. Last Non-Zero

    fast=4 has value 12. Swap with slow=2.

  5. Done

    All non-zeros moved to front, zeros at end.