Home
DSA Patterns
Two Pointers
Squares of a Sorted Array
Squares of a Sorted Array (LeetCode 977)
Return the sorted squares of a sorted array in O(n): the largest square is always at one end, so fill the output from the back with two pointers.
Problem Understanding
Squares of a Sorted Array: given an array sorted in non-decreasing order, return the squares of each number, also sorted.
Example: [-4, -1, 0, 3, 10] returns [0, 1, 9, 16, 100]. Squaring in place gives [16, 1, 0, 9, 100], which is no longer sorted because of the negatives.
The Intuition: The Biggest Square Is on the Outside
Negatives break the order: -4 is smaller than 3, but its square is larger. What stays true is that squares grow with absolute value, and in a sorted array the largest absolute values sit at the two ends.
So use two pointers, one at each end, and build the answer from the largest square down:
- Compare
|nums[lo]|and|nums[hi]|. - Write the larger one's square to the last free slot of the output.
- Move that pointer inward and repeat.
It is the same back-to-front fill as Merge Sorted Array: two sorted runs (the negatives reversed, and the positives) merged from the largest end.
Squares of a Sorted Array Visualization
THE LARGEST SQUARE LEFT IS ALWAYS AT lo OR hi
POINTERS AT THE ENDS
nums
lo
0
1
2
3
hi
4
output
0
1
2
3
4
lo
0
hi
4
k
:
The largest square is at one end or the other. Point lo and hi at the ends and fill the output from the back
Next
|10| ≥ |-4|: 10² = 100 is the largest left. Put it at 4, move hi left
nums
The values still between lo and hi are shaded. Each step, the end with the larger absolute value wins: its square drops into the output at k, which fills from the right.
Squares of a Sorted Array Dry Run
Input: [-4, -1, 0, 3, 10]
| lo | hi | Compare | Write | k |
|---|---|---|---|---|
| -4 | 10 | 4 < 10 | 100 | 4 |
| -4 | 3 | 4 > 3 | 16 | 3 |
| -1 | 3 | 1 < 3 | 9 | 2 |
| -1 | 0 | 1 > 0 | 1 | 1 |
| 0 | 0 | equal | 0 | 0 |
Result: [0, 1, 9, 16, 100].
Squares of a Sorted Array LeetCode Solution
Two pointers from the ends, filling the output from the back.
Squares of a Sorted Array Code
1var sortedSquares = function(nums) {2 const n = nums.length;3 const out = new Array(n);4 let lo = 0, hi = n - 1;5 for (let k = n - 1; k >= 0; k--) {6 if (Math.abs(nums[lo]) > Math.abs(nums[hi])) {7 out[k] = nums[lo] * nums[lo];8 lo++;9 } else {10 out[k] = nums[hi] * nums[hi];11 hi--;12 }13 }14 return out;15};
Squares of a Sorted Array in Python, Java, C++, TypeScript and C
Squares of a Sorted Array in Python
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
n = len(nums)
out = [0] * n
lo, hi = 0, n - 1
for k in range(n - 1, -1, -1):
if abs(nums[lo]) > abs(nums[hi]):
out[k] = nums[lo] ** 2
lo += 1
else:
out[k] = nums[hi] ** 2
hi -= 1
return outSquares of a Sorted Array in Java
class Solution {
public int[] sortedSquares(int[] nums) {
int n = nums.length;
int[] out = new int[n];
int lo = 0, hi = n - 1;
for (int k = n - 1; k >= 0; k--) {
if (Math.abs(nums[lo]) > Math.abs(nums[hi])) {
out[k] = nums[lo] * nums[lo];
lo++;
} else {
out[k] = nums[hi] * nums[hi];
hi--;
}
}
return out;
}
}Squares of a Sorted Array in C++
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int n = nums.size();
vector<int> out(n);
int lo = 0, hi = n - 1;
for (int k = n - 1; k >= 0; k--) {
if (abs(nums[lo]) > abs(nums[hi])) {
out[k] = nums[lo] * nums[lo];
lo++;
} else {
out[k] = nums[hi] * nums[hi];
hi--;
}
}
return out;
}
};Squares of a Sorted Array in TypeScript
function sortedSquares(nums: number[]): number[] {
const n = nums.length;
const out: number[] = new Array(n);
let lo = 0, hi = n - 1;
for (let k = n - 1; k >= 0; k--) {
if (Math.abs(nums[lo]) > Math.abs(nums[hi])) {
out[k] = nums[lo] * nums[lo];
lo++;
} else {
out[k] = nums[hi] * nums[hi];
hi--;
}
}
return out;
}Squares of a Sorted Array in C
int* sortedSquares(int* nums, int numsSize, int* returnSize) {
int* out = malloc(numsSize * sizeof(int));
int lo = 0, hi = numsSize - 1;
for (int k = numsSize - 1; k >= 0; k--) {
if (abs(nums[lo]) > abs(nums[hi])) {
out[k] = nums[lo] * nums[lo];
lo++;
} else {
out[k] = nums[hi] * nums[hi];
hi--;
}
}
*returnSize = numsSize;
return out;
}Edge Cases & Common Mistakes
- All non-negative: the right end always wins; the squares keep their order.
- All negative: the left end always wins; the output is the input reversed and squared.
- Filling from the front: the ends give the largest squares, so writing them at the front produces a descending array.
- Comparing values instead of absolute values:
-4 < 3, but16 > 9.
Summary
Squares grow with absolute value, so the largest square left is always at one end of the sorted array. Compare the two ends, write the larger square into the output from the back, and move that pointer inward. O(n) time.
More practice: DSA interview questions with answers · patterns cheat sheet
