You are given an array of integers that may contain duplicates. Consider only the set of distinct values that appear in the array. Sort those distinct values in strictly increasing order and report the value at position k (1-indexed) in that sorted order.
If the array contains fewer than k distinct values, print -1 instead.
Line 1: an integer n — the number of elements in the array.
Line 2: an integer k — the 1-indexed rank to report.
Line 3: n space-separated integers — the array elements.
A single line containing the k-th smallest distinct value, or -1 if fewer than k distinct values exist.
Example 1
Input
6 3 5 1 3 3 5 2
Expected
3
Explanation
The distinct values are {1, 2, 3, 5}; sorted ascending they are 1, 2, 3, 5. The 3rd is 3.
Example 2
Input
4 5 7 7 7 7
Expected
-1
Explanation
There is only one distinct value (7), which is fewer than k = 5, so 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 →