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 two pointers at both ends of the sorted array. Compare the absolute values (squares) at each pointer, place the larger square at the end of the result array, and move that pointer inward. Fill the result from right to left.
In a sorted array with negatives, the largest squares are at the extremes. By comparing from both ends and filling the result backwards, we produce a sorted output in one pass.
Updated Feb 2026