easyArrayTwo PointersTwo Pointers

Move Zeroes

easyTime: O(n)Space: O(1)

Recognize the pattern

move elements in-placepartition arraymaintain relative order

Brute force idea

If you approach Move Zeroes in the most literal way possible, you get this: Create new array, copy non-zeros then fill zeros. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

Better approach

The real unlock in Move Zeroes comes when you notice this: Two pointers: slow marks next non-zero position, fast scans ahead. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Key invariant

The compass for Move Zeroes is this: Everything before the slow pointer is correctly placed (non-zero values in order). As long as that statement keeps holding, you can trust the steps built on top of it.

Watch out for

One easy way to drift off course in Move Zeroes is this: Swapping instead of overwriting — swap preserves zeros naturally without a second pass. The fix is usually to return to the meaning of each move, not just the steps themselves.

Two Pointers Pattern