25. Squares of a Sorted Array

EasyArrayArray

Given an integer array nums sorted in non-decreasing order, return a new array of the squares of each number, also sorted in non-decreasing order.

Input: A sorted integer array nums.

Output: Array of squared values sorted in non-decreasing order.

Examples

Example 1
Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: Squares: [16,1,0,9,100]. Sorted: [0,1,9,16,100].
Example 2
Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]
Explanation: Squares: [49,9,4,9,121]. Sorted: [4,9,9,49,121].
Example 3
Input: [0,1,2,3,4]
Output: [0,1,4,9,16]
Explanation: All non-negative. Squares already in order.

Constraints

Asked by

MetaBloombergAmazonGoogleAdobeAccenture
Solve this problem in the editor →