977. Squares of a Sorted Array
🟩 Easy
Given an integer array nums
sorted in non-decreasing order, 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] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [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
-104 <= nums[i] <= 10^4
nums
is sorted in non-decreasing order.
Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n)
solution using a different approach?
Solution
My Solution
Optimal Solution (Two Pointers)
Approach
This solution uses a two-pointer technique to build the sorted squared array:
Key Observation:
In a sorted array, largest squares will come from either:
Largest positive numbers (at the end)
Largest absolute negative numbers (at the start)
Two-Pointer Strategy:
Left pointer at start (for negative numbers)
Right pointer at end (for positive numbers)
Compare absolute values to decide which square is larger
Result Construction:
Build result array from end to start
Place larger squares first
Move pointers accordingly
Complexity Analysis
Time Complexity: O(n)
Single pass through the array
Each element processed exactly once
No sorting required
All operations are O(1)
Space Complexity: O(n)
Result array of size n
Only constant extra space besides output:
Two pointers (left, right)
Loop counter
Temporary variables for calculations
Why it works
Array Properties:
Input array is sorted
Squares of numbers follow a U-shaped pattern
Largest squares are at the extremes
Two-Pointer Benefits:
No need to sort after squaring
Directly builds sorted result
Handles both positive and negative numbers efficiently
Optimization Details:
Pre-allocating result array avoids resizing
Building from end eliminates need for reversal
Absolute value comparison ensures correct ordering
Last updated
Was this helpful?