SDE Path

Kth Largest Element in an Array

Medium

Kth Largest Element in an Array

Find the Kth largest element in an unsorted array using a heap.

Input format

The first line contains N and K. The second line contains N space-separated integers.

Output format

Print the Kth largest element (1-indexed).

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
6 2
3 2 1 5 6 4
Expected output
5
Example 2
Input
9 4
3 2 3 1 2 4 5 5 6
Expected output
4