-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay93.js
More file actions
34 lines (26 loc) · 1.04 KB
/
Day93.js
File metadata and controls
34 lines (26 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//* Write a JavaScript function that takes an array of integers and returns the array of the squares of each number, but sorted in non-decreasing order.
//* For example, given the array [-4, -1, 0, 3, 10], your function should return [0, 1, 9, 16, 100].
//* Requirements:
//* The function should handle both positive and negative numbers.
// *The input array may not be sorted.
//* Do not use the Array's sort() method directly on the result array after squaring the elements.
// *Bonus Challenge: Try to achieve this with a time complexity better than O(n log n).
function sortedSquares(nums) {
let result = new Array(nums.length);
let left = 0;
let right = nums.length - 1;
let position = nums.length - 1;
while (left <= right) {
if (Math.abs(nums[left]) > Math.abs(nums[right])) {
result[position] = nums[left] * nums[left];
left++;
} else {
result[position] = nums[right] * nums[right];
right--;
}
position--;
}
return result;
}
// Example usage
console.log(sortedSquares([-4, -1, 0, 3, 10])); // Output: [0, 1, 9, 16, 100]