Kth Largest Element in an Array

MediumHeap
Next
HeapQuickselectSorting

Find the k-th largest element in an unsorted array. It is the k-th largest in sorted order, not the k-th distinct value.

Input

Line 1: n and k. Line 2: n integers.

Output

Print the k-th largest element.

Constraints

1 <= k <= n <= 10^5 -10^4 <= each value <= 10^4

Example 1
Input
6 2
3 2 1 5 6 4
Output
5

Sorted descending: 6 5 4 3 2 1, so the 2nd largest is 5.

Hints

Kth Largest Element in an Array - Medium Heap Problem | Skill Shiksha