2Brute2Best
πŸ”₯0

Squares of a Sorted Array

EasyΒ·LeetCode #977 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given an integer array nums sorted from smallest to largest. It can contain negative numbers.

Return a new array with the square of every number, also sorted from smallest to largest.

Follow-up: squaring and then sorting is easy. Can you do it in O(n) time?

Input

An integer array nums, sorted in non-decreasing order.

Output

An array of the squares of nums, sorted in non-decreasing order.

Example 1
Input: nums = [-4, -1, 0, 3, 10]
Output: [0, 1, 9, 16, 100]

The squares are [16, 1, 0, 9, 100]. Sorted, that's [0, 1, 9, 16, 100].

Example 2
Input: nums = [-7, -3, 2, 3, 11]
Output: [4, 9, 9, 49, 121]

The squares are [49, 9, 4, 9, 121]. Sorted, that's [4, 9, 9, 49, 121].

Constraints
  • β€’ 1 ≀ nums.length ≀ 10^4
  • β€’ βˆ’10^4 ≀ nums[i] ≀ 10^4
  • β€’ nums is sorted in non-decreasing order
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œsorted array (with negatives)”
β†’ The extreme values sit at the two ends β†’ look at both ends.
β€œsquares”
β†’ Big negative and big positive both give big squares, so the two ends compete for "biggest".
β€œsorted output, O(n)”
β†’ No sorting allowed β†’ build the output in order, one end at a time.
03

Approach Complexity Ladder

Brute Force β€” square, then sortO(N log N)O(N)

Square every number, then sort the result

Optimal β€” two ends compete, fill from the backO(N)O(N)

Bigger square of the two ends goes into the last empty slot

Brute Force Intuition

Square each number into a new array, then sort it.

Recognition rule for this sub-pattern:

Start at both ends of a sorted (or symmetric) structure. Each step, move the side that cannot do better.
Remember this Β· Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.