Lesson 1 of 5
Live algorithm trace
Stable in-place compaction
Complete execution1 of 9
Write marks the next position for a non-zero value.
1
write = 02
scan each x from left to right3
if x != 0: nums[write] = x; write += 14
while write < length: nums[write] = 0; write += 15
return numswrite = 0
A sandstorm scattered empty carts (0) through the caravan. Move every loaded cart forward while keeping their order in place, with no second caravan.