easy +10 pts

Squares of Sorted Array

Return a sorted array of the squares of each number in a non-decreasing input array.

Given an integer array `nums` sorted in non-decreasing order, return an array of the squares of each number, also sorted in non-decreasing order. Implement the function `sorted_squares(nums)` in Python. Your solution should be efficient enough to handle large inputs. A two-pointer approach is expected, but any correct solution within the constraints is acceptable.

Constraints

1 <= len(nums) <= 10^5 -10^4 <= nums[i] <= 10^4 Input is sorted in non-decreasing order. Output must be a list of integers sorted in non-decreasing order.

Example

>>> sorted_squares([-4,-1,0,3,10])
[0, 1, 9, 16, 100]

>>> sorted_squares([-7,-3,2,3,11])
[4, 9, 9, 49, 121]
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Try using two pointers: one at the start and one at the end.
Compare the absolute values of the two pointer elements to decide which square is larger.
Fill the result array from the end to the beginning to avoid reversing later.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.