Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Use a slow pointer to track where the next non-zero element should go. The fast pointer scans the array and whenever it finds a non-zero element, swap it with the element at the slow pointer position. This partitions the array into non-zeroes followed by zeroes.
Swapping non-zero elements to the slow pointer position preserves their relative order while naturally pushing zeroes to the end.
Updated Feb 2026