Back to all solutions

#977 - Squares of a Sorted Array

Problem Description

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

Solution

/**
 * @param {number[]} A
 * @return {number[]}
 */
var sortedSquares = function(A) {
  return A.map(n => Math.pow(n, 2)).sort((a, b) => a - b);
};