A sensor logs n signed deviation readings from a target. Order the readings primarily by their absolute magnitude, from smallest magnitude to largest. When two readings have the same absolute magnitude, the one with the smaller signed value comes first (so a negative reading precedes the positive reading of equal magnitude). Report the reading at position k in this ordering (positions are counted from 1).
Line 1: two integers n and k.
Line 2: n space-separated integers, the deviation readings.
A single integer: the k-th reading in the described order.
Example 1
Input
5 2 3 -1 -2 4 -1
Expected
-1
Explanation
By magnitude then signed value the order is -1, -1, -2, 3, 4. The 2nd element is -1.
Example 2
Input
3 3 5 5 5
Expected
5
Explanation
All three readings are 5, so the order is 5, 5, 5 and the 3rd is 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →