← Visualizers
Opposite-Ends Two Pointers LeetCode 977

Squares of a Sorted Array / visualized

Square every element and return the result sorted — in one linear pass, no re-sorting. Because negatives square into large positives, the biggest square is always at one of the two ends. Compare left and right, place the winner at the back of the result, and work inward.

Execution

idle
input (sorted)
result (filling from the back)
Press Run to begin.
0 / 0
Speed

Java · running line

left right placed in result
O(n) time · O(n) output space  ·  filling back-to-front is what avoids an O(n log n) re-sort.