75 / 75
E

Move Zeroes

#75
arraytwo-pointers

Use write pointer to compact non-zero elements, then fill remaining positions with zeros.

Example:

Input:nums=[0,1,0,3,12]
Output:[1,3,12,0,0] (in-place)

Common Mistakes:

  • Using extra space (not in-place)
  • Not maintaining relative order of non-zeros
  • Forgetting to fill trailing positions with zeros
  • Overcomplicating with swaps when write pointer suffices

Notes:

Two-pointer technique. Time O(n), Space O(1). Alternative: swap non-zeros with zeros as you go, but less intuitive.

75/75
Move Zeroes