Squares of a Sorted Array
An easy Two Pointers problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Two Pointers
- Sheets
- 1
- Core for
- 10 roles
- Platform
- LeetCode
The problem
Given a sorted array of integers, return an array of the squares of each number sorted in non-decreasing order.
Example 1
- Input
- nums = [-4, -1, 0, 3, 10]
- Output
- [0, 1, 9, 16, 100]
- Why
- Squares are [16, 1, 0, 9, 100], sorted to [0, 1, 9, 16, 100].
Example 2
- Input
- nums = [-7, -3, 2, 3, 11]
- Output
- [4, 9, 9, 49, 121]
Constraints
- 1 <= nums.length <= 10^4
- -10^4 <= nums[i] <= 10^4
- nums is sorted in non-decreasing order
How to think about it
Updated 2026-09-09Squaring a sorted sequence creates a V-shape: the largest values reside strictly at the outer extremes, where absolute values are greatest. Comparing the two ends always uncovers the next largest square, letting you populate the destination array backwards in a single coordinated pass.
Approaches, worst first
Square and sort
time O(n log n) · space O(n)
Square each number in place or into a new list, then run a standard sorting algorithm. Correct and trivial to write, but it completely discards the partial ordering already present in the input.
Inward two-pointer fillWrite this one
time O(n) · space O(n)
Position pointers at both ends of the array and allocate a result buffer of size n. At each step, square both ends, place the larger square at the current tail of the result buffer, and advance that pointer inward.
Where people lose marks · 3
- Filling the output array forward from index 0. The two pointers surface the largest squares first, not the smallest; writing forward requires an extra reverse or binary search for the inflection point.
- Comparing squared values rather than absolute values before squaring. While math is equivalent, squaring large numbers prematurely can cause arithmetic overflows in fixed-width numeric environments.
- Moving both pointers when values tie at the ends. Only one pointer must advance per written element to ensure no elements are dropped.
The theory behind it
Two Pointers — the ground this problem stands on. All Two Pointers problems
What Two Pointers is
Two pointers is a search coordinated by two fingers placed on different entries of an ordered row, stepping inward toward each other or advancing at different speeds. Instead of scanning with nested loops that re-examine identical pairs, each pointer moves unidirectionally based on comparisons. Because every movement permanently prunes bad candidate combinations, the search inspects the collection in a single joint pass.
When to reach for it
Reach for two pointers when problems require finding index pairs, reversing sequences, trapping rainwater, or filtering duplicates in sorted arrays. When an input is already sorted and an exhaustive search takes quadratic time, opposing pointers meeting in the center often solve it linearly. Fast and slow pointer variations also detect cycles or locate midpoint nodes across linked sequences.
How the pattern works
Initialize pointers at opposing edges for convergent searches, or start both at the beginning to move at different speeds. Under a convergent setup, calculate current pair metrics; if the sum falls short of your target, advance the lower pointer rightward to increase value, and if it exceeds your target, shift the upper pointer leftward to decrease value. Each pointer move throws away only pairs that were already known to be wrong, so the right answer is never missed.
What each operation costs
| Operation | Time |
|---|---|
| converging scan across sorted collection | O(n) |
| slow and fast cycle traversal | O(n) |
| auxiliary memory overhead for pointer markers | O(1) |
What usually goes wrong with Two Pointers
- Applying convergent opposite-end pointers to an unsorted collection without realizing that sorting was a necessary precondition for direction-based pruning.
- Allowing pointers to cross past one another or crash into identical indices when matching duplicate values, skipping the termination condition.
- Forgetting to advance pointers past repeated values during duplicate-skipping loops, creating unexpected infinite iterations on matching elements.
Which roles need this problem
Two Pointers is a core topic for these 10 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 15 more roles, including SDE / Backend Engineer, ML Engineer, Graphics Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Two Pointers problems
Problem set and role mapping as of .