A network of automated weather balloons phones in an elevation reading (in meters) every time it completes a lap; the same elevation value can be phoned in more than once during a flight. Mission control wants the k-th highest DISTINCT elevation value that was ever reported — two reports of the same elevation should only count once when ranking values. Given every reading and an integer k, report that value, or -1 if fewer than k distinct elevation values were ever reported.
Print a single integer: the k-th highest DISTINCT value among a_1..a_n, where the highest distinct value has rank 1. Print -1 if there are fewer than k distinct values.
Example 1
Input
5 2 300 300 250 500 500
Expected
300
Explanation
The distinct elevations reported are 500, 300, and 250. Ranked from highest to lowest that is 500 (rank 1), 300 (rank 2), 250 (rank 3). k=2 asks for rank 2, which is 300.
Example 2
Input
3 5 100 100 100
Expected
-1
Explanation
Every reading is 100, so there is only one distinct elevation value ever reported. Since k=5 asks for a 5th-highest distinct value and only 1 distinct value exists, the answer is -1.
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 →