Explain the in-place approach.
Skip to solutionKEEP THE
easyDSA
How do you move all zeroes to the end of an array in place?
691 views
01
Understand the problem
arraystwo-pointers
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Use a write pointer: iterate, copying each non-zero to the write index and advancing it; afterward fill the rest with zeros. O(n) time, O(1) space, preserving order of non-zeros.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Write-pointer (stable)
Run Playgrounddef move_zeroes(nums):
write = 0
for x in nums:
if x != 0:
nums[write] = x
write += 1
for i in range(write, len(nums)):
nums[i] = 0
return nums
# --- demo ---
print(move_zeroes([0, 1, 0, 3, 12])) # [1, 3, 12, 0, 0]05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 13 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.