Move zeros preserving order21.02
Stability: the first non-zero stays before later non-zeros. Use a write pointer scanning left to right, not random swaps with the end.
Swapping a[i] with a[j] from the right can reorder non-zeros. That fails this skill even if zeros end up last.
Same skill, three traces. Read all three before you try it.
01stable - given
a = [0, 4, 0, 1, 3]- walk
4then1then3- result
[4, 1, 3, 0, 0]
02unstable swap-from-end - given
a = [0, 4, 0, 1, 3]- walk
might become
[3, 4, 1, 0, 0]- result
- wrong if order required
03already ordered - given
a = [4, 1, 0, 0]- walk
wcopies4,1then fill- result
[4, 1, 0, 0]
stableZerosToEnd(a).a is an array of integers.Move zeros to the end with a left-to-right write pointer so non-zero order stays. Return
a.On
[0, 4, 0, 1, 3] the prefix must be 4, 1, 3, not any other order.constraintsa is non-empty.
- Move Zeroes0/3 skills ready · arrays · easyopen
- Move Zeroes0/3 skills ready · arrays · easyopen
Every Check with Awel is frozen here. Open one to read the code or load it back into the editor.