Sorted Squared Array
Sorted Squared Array
// O(nlogn) time | O(n) space
function sortedSquaredArray(array) {
const sortedSquares = new Array(array.length).fill(0);
for (let i = 0; i < array.length; i++) {
const element = array[i];
sortedSquares[i] = element * element;
}
sortedSquaredArray.sort((a, b) => a - b)
return sortedSquares;
}
Last updated