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] <= 104
nums
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:
We need to find the squares of numbers in a sorted list and then sort the squares. The brute force method will individually compute the square of each number and then sort the entire collection of squares.
Here's how the algorithm would work step-by-step:
def squares_of_a_sorted_array_brute_force(numbers):
squares = []
# Calculate the square of each number
for number in numbers:
square = number * number
squares.append(square)
# The next step is to sort the squares
squares.sort()
return squares
Since the input numbers are sorted, we can use this to our advantage. The core idea is to realize that the largest squares will be at the edges of the input, either from the most negative or most positive numbers. We then work inward from the edges to build the sorted squares.
Here's how the algorithm would work step-by-step:
def squares_of_a_sorted_array(numbers):
number_of_elements = len(numbers)
squared_numbers = [0] * number_of_elements
left_pointer = 0
right_pointer = number_of_elements - 1
result_index = number_of_elements - 1
while left_pointer <= right_pointer:
# Determine which square is larger.
if abs(numbers[left_pointer]) > abs(numbers[right_pointer]):
squared_numbers[result_index] = numbers[left_pointer] ** 2
left_pointer += 1
else:
squared_numbers[result_index] = numbers[right_pointer] ** 2
# Decrement, since we placed the largest at end.
right_pointer -= 1
# Fill array from the end to the beginning.
result_index -= 1
return squared_numbers
Case | How to Handle |
---|---|
Null or empty input array | Return an empty array or throw an IllegalArgumentException, depending on requirements, ensuring to handle the absence of input gracefully. |
Input array with only one element | Return an array containing the square of the single element, as it's already sorted by default. |
Input array with all identical values (e.g., [2, 2, 2]) | The squaring and sorting logic should handle identical values correctly, resulting in an array of the squares of the identical values, also sorted. |
Input array with a large range of negative and positive numbers (e.g., [-1000, -1, 0, 1, 1000]) | The solution must correctly square negative numbers and then efficiently sort the resulting squared values, considering magnitude changes upon squaring. |
Input array with extremely large numbers that, when squared, could lead to integer overflow | Use a data type with a larger range (e.g., long) or consider using arbitrary-precision arithmetic to prevent overflow issues during the squaring operation. |
Input array containing zeros | Zeroes will be squared to zero and should be placed correctly in the sorted output array. |
Very large input array to test for performance | The algorithm should have a time complexity of O(n log n) or better (like O(n)) to efficiently handle large datasets where 'n' is the size of the array. |
Array already sorted with all positive numbers | The result will be the square of each element sorted in increasing order, no modifications necessary. |