Squares of Sorted Array

easy

Return squares of sorted array in sorted order (handles negatives)

Squares of Sorted Array

Key Insight

Largest squares at ends (negatives). Compare ends, fill result from back.

Step 1Setup
(-4)²=16, 10²=100
left
-4
0
-1
1
0
2
3
3
right
10
4
converge

Array has negatives. Largest squares are at the ends.

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Squares of Sorted Array

Largest squares at ends (negatives). Compare ends, fill result from back.

  1. Setup

    Array has negatives. Largest squares are at the ends.

  2. Compare Squares

    (-4)²=16 vs 10²=100. 100 is larger, place at end.

  3. Next Comparison

    (-4)²=16 vs 3²=9. 16 is larger.

  4. Continue

    (-1)²=1 vs 3²=9. 9 is larger.

  5. Result

    Sorted squares: [0, 1, 9, 16, 100]