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 <= 104-104 <= nums[i] <= 104nums is sorted in non-decreasing order.O(n) solution using a different approach?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given a row of numbers that are already ordered from smallest to largest, and your task is to figure out what those numbers look like after you multiply each of them by themselves. The brute force approach simply looks at every single number one by one, performs the multiplication, and collects all the new resulting values together.
Here's how the algorithm would work step-by-step:
def sorted_squares_brute_force(sorted_numbers):
squared_collection = []
# Square every individual number so we can collect all transformed values
for current_number in sorted_numbers:
squared_value = current_number * current_number
squared_collection.append(squared_value)
# Sort the collection because squaring negative numbers disrupts the original order
squared_collection.sort()
return squared_collectionInstead of squaring all the numbers first and trying to sort them from scratch, the optimal approach works backwards by comparing the most extreme values. Since the biggest squared numbers will always be sitting at the very beginning or the very end of the line, we can simply pick the larger one, place it into its final spot, and move inward.
Here's how the algorithm would work step-by-step:
def sorted_squares(sorted_numbers):
array_length = len(sorted_numbers)
result_array = [0] * array_length
left_pointer = 0
right_pointer = array_length - 1
insertion_index = array_length - 1
# Compare outer extremes because the largest squares always reside at the array ends.
while left_pointer <= right_pointer:
left_square = sorted_numbers[left_pointer] ** 2
right_square = sorted_numbers[right_pointer] ** 2
# Place the larger square at the current end and shrink the active boundary.
if left_square > right_square:
result_array[insertion_index] = left_square
left_pointer += 1
else:
result_array[insertion_index] = right_square
right_pointer -= 1
insertion_index -= 1
# Return the fully populated array containing sorted squared values.
return result_array| Case | How to Handle |
|---|---|
| Empty input array | Return the empty array immediately without entering the two-pointer loop. |
| Array with a single element | The loop processes the single element, squares it, and returns a new array with that single squared value. |
| Array containing all negative numbers | The two-pointer approach correctly starts from the largest absolute values at the left and populates the result array from right to left in ascending order. |
| Array containing all positive numbers | The right pointer starts at the end with the largest value, and the pointers move inward while filling the result array correctly. |
| Array containing zeros | Zeros square to zero and are handled naturally by the pointer comparisons without causing division by zero or index out of bounds. |
| Array with mixed positive and negative numbers containing duplicates of absolute values | The equal condition in the two-pointer comparison safely processes either the negative or positive duplicate first while maintaining correct output order. |
| Maximum-sized inputs with extreme boundary integer values | The algorithm scales efficiently in linear O(N) time using two pointers, though squaring the minimum 32-bit signed integer requires 64-bit integers to prevent overflow. |