SDE Path

Kth Smallest Element in an Array

Easy

Kth Smallest Element in an Array

Find the Kth smallest 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 smallest 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 3
3 2 1 5 6 4
Expected output
3
Example 2
Input
5 1
3 1 4 2 2
Expected output
1