Squares of a Sorted Array
The key insight: given a sorted array that may contain negative numbers, the square of a negative number is positive and large in magnitude when the number has large absolute value. Since the array is sorted, the largest square could come from either end (the most negative or the most positive). Use two pointers starting at both ends, compare the squared magnitudes, and fill the result array from the back (largest square first) since either end could hold the largest absolute value.
Details
Animated on: Given nums = [-4, -1, 0, 3, 10], return the squares of each number sorted in non-decreasing order. Answer: [0, 1, 9, 16, 100].
Problem:Squares of a Sorted ArrayEasy
Two Pointers
step 1 / 12
left = 0, right = n - 1 = 4, pos = n - 1 = 4: initialize two pointers at both ends and a result array position at the back. The squared magnitudes are 16 and 100 respectively.
- left=0· arr[0] = -4
- right=4· arr[4] = 10
1FUNCTION sortedSquares(nums):2 n = length of nums3 result = a new array of length n4 left = 0, right = n - 15 pos = n - 16 WHILE left <= right:7 leftSquare = nums[left] * nums[left]8 rightSquare = nums[right] * nums[right]9 IF leftSquare > rightSquare:10 result[pos] = leftSquare11 left = left + 112 ELSE:13 result[pos] = rightSquare14 right = right - 115 pos = pos - 1
← / → step · space play · Home restart
Every step in words
Show all 12 steps
- left = 0, right = n - 1 = 4, pos = n - 1 = 4: initialize two pointers at both ends and a result array position at the back. The squared magnitudes are 16 and 100 respectively.
- leftSquare = nums[left]^2 = (-4)^2 = 16, rightSquare = nums[right]^2 = 10^2 = 100. Compare: 16 < 100, so the larger square is at the right end.
- result[pos=4] = rightSquare = 100: place the larger square at the back of the result array. Decrease right to 3 and pos to 3. Result so far: [_, _, _, _, 100].
- leftSquare = (-4)^2 = 16, rightSquare = 3^2 = 9. Compare: 16 > 9, so the larger square is at the left end.
- result[pos=3] = leftSquare = 16: place the left square at the current back position. Increase left to 1 and decrease pos to 2. Result so far: [_, _, _, 16, 100].
- leftSquare = (-1)^2 = 1, rightSquare = 3^2 = 9. Compare: 1 < 9, so the larger square is at the right end.
- result[pos=2] = rightSquare = 9: place the right square at position 2. Decrease right to 2 and pos to 1. Result so far: [_, _, 9, 16, 100].
- leftSquare = (-1)^2 = 1, rightSquare = 0^2 = 0. Compare: 1 > 0, so the larger square is at the left end.
- result[pos=1] = leftSquare = 1: place the left square at position 1. Increase left to 2 and decrease pos to 0. Result so far: [_, 1, 9, 16, 100].
- leftSquare = 0^2 = 0, rightSquare = 0^2 = 0. Compare: 0 > 0 is false, so the else branch runs — the right square is taken.
- result[pos=0] = rightSquare = 0: place the final square. right decreases to 1; left stays at 2. Now left(2) > right(1), so the loop terminates.
- Final result: [0, 1, 9, 16, 100]. All squares are placed in non-decreasing order. Return the result.
Now solve it yourself
Watching is the first half. The second is writing it without the animation.