Loading...
Loading...
In a physics lab, a sensor records how far each reading is from the set temperature, as a whole number. Readings below the set point are negative. The readings are stored in sorted order. For the error report, the lab needs the square of every reading, also in sorted order. Print the sorted squares in one pass, without sorting again.
Example
Input
5 -4 -1 0 3 10
Output
0 1 9 16 100
The squares are 16, 1, 0, 9 and 100. In sorted order: 0 1 9 16 100.
Try the problem first. Open one hint at a time.
The largest square comes from one of the two ends: the most negative reading or the largest positive one.
Keep one index at each end and fill the answer from the back.
Compare the two end squares, write the bigger one at the current back position, and move that index inward.
Use long, because a reading of 10^9 squared is 10^18.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor