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
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution