Squared Deviations
A greenhouse sensor logs how far each reading is from the ideal temperature: negative for too cold, positive for too warm. The log is sorted from lowest to highest, for example [-7, -3, 0, 2, 5].
The climate system punishes big misses more, so its cost is the square of each deviation. It wants those costs sorted from smallest to largest.
Your task
Write squaredSorted(deviations). Return a new list with the square of every number, sorted from smallest to largest.
Aim for a single pass over the list, and use the fact that the input is already sorted.
squaredSorted(deviations: int[]) → int[]
Examples
deviations = [-7, -3, 0, 2, 5]
[0, 4, 9, 25, 49]
The squares in input order are 49, 9, 0, 4, 25. Sorted, they are 0, 4, 9, 25, 49. The biggest squares come from the two ends of the input, -7 and 5.
deviations = [-4, -1, 0, 3, 10]
[0, 1, 9, 16, 100]
The squares in input order are 16, 1, 0, 9, 100. The largest, 100, comes from the right end, and the next largest, 16, comes from the left end.
Constraints
- 0 ≤ deviations.length ≤ 10,000
- -10,000 ≤ deviations[i] ≤ 10,000
- deviations is sorted in non-decreasing order.
Start with brute force
Square every number and then sort the result. This is correct and takes only a few lines.
The sort costs O(n log n) and ignores that the input was already sorted, which tells you where the big values are.
Hints
0 of 4 shown- Hint 1
Squaring breaks the sorted order. Where in the input do the largest squares come from?
- Hint 2
The biggest square is always at one end: the most negative or the most positive number. The middle holds the numbers closest to zero.
- Hint 3
So build the answer from largest to smallest, taking the bigger end square each time.
- Hint 4
Put
leftandrightpointers at the ends and fill the result from the back. Each step, write the larger end square at the current back position and move that pointer inward.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The largest square is always at an end, so fill the answer from the back.
Trace [-7, -3, 0, 2, 5] into 5 slots:
- The ends are -7 and 5. Since 49 > 25, slot 4 gets 49 and
leftmoves to -3. - The ends are -3 and 5. Since 9 < 25, slot 3 gets 25 and
rightmoves to 2. - The ends are -3 and 2. Since 9 > 4, slot 2 gets 9 and
leftmoves to 0. - The ends are 0 and 2. Since 0 < 4, slot 1 gets 4 and
rightmoves to 0. - Both pointers are on 0, so slot 0 gets 0. The result is
[0, 4, 9, 25, 49].
Each step places one value and moves one pointer, so the loop runs n times.
function squaredSorted(deviations) {
const n = deviations.length;
const result = new Array(n);
let left = 0, right = n - 1;
for (let pos = n - 1; pos >= 0; pos--) {
const l = deviations[left] * deviations[left];
const r = deviations[right] * deviations[right];
if (l > r) {
result[pos] = l;
left++;
} else {
result[pos] = r;
right--;
}
}
return result;
}def squared_sorted(deviations):
n = len(deviations)
result = [0] * n
left, right = 0, n - 1
for pos in range(n - 1, -1, -1):
l = deviations[left] ** 2
r = deviations[right] ** 2
if l > r:
result[pos] = l
left += 1
else:
result[pos] = r
right -= 1
return resultRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.