Squares of a Sorted Array

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Two Pointers — Two Indices, One Rule

Move left/right based on a condition instead of checking all pairs.

int L = 0, R = n - 1;
while (L < R) {
    int sum = a[L] + a[R];
    if (sum == target) { L++; R--; }
    else if (sum < target) L++;
    else R--;
}
Two Pointers

Squares of a Sorted Array

Easy
LeetCode
6/7
EasyTwo PointersLeetCode #977

Squares of Sorted Array

Negatives square large too. Two pointers from ends; fill result from back.

Examples

Example 1

Input

5
-4 -1 0 3 10

Output

0 1 9 16 100
Example 2

Input

5
-7 -3 2 3 11

Output

4 9 9 49 121

Stuck? Take a nudge

C++17