272. The Kth Largest Value
Foundation1000 ms256 MBSolved by 0%
Print the kth largest of n integers, counting from 1. So k = 1 is the
maximum.
Duplicates count separately: in 7 7 7 7 the 4th largest is 7.
This is problem 243 from the other end. Sorting descending and reading position
k - 1 works, and so does sorting ascending and reading position n - k.
Getting that second index right is the entire exercise.
Constraints - `1 ≤ k ≤ n ≤ 200000` - `-1000000000 ≤ a[i] ≤ 1000000000`
Input
The first line contains two integers n and k.
The second line contains n integers.
Output
Print one integer, the kth largest value.
Input6 2
3 2 1 5 6 4
Output5
Noteis the second largest in an unsorted array
Input1 1
1
Output1
Noteis a single element
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.