#K3071. K-th Smallest Element Finder

    ID: 24877 Type: Default 1000ms 256MiB

K-th Smallest Element Finder

K-th Smallest Element Finder

You are given T test cases. For each test case, you are given a list of n integers and an integer k. Your task is to find the k-th smallest element in the list.

The k-th smallest element is defined as the element that appears in the k-th position when the list is sorted in non-decreasing order. Mathematically, if the sorted order of the list is \(a_1, a_2, ..., a_n\), you need to output \(a_k\).

Note: It is guaranteed that \(1 \le k \le n\). The integers can be both positive and negative.

Use efficient methods (e.g., a heap) where possible to help with performance on large inputs.

inputFormat

The input is given via standard input (stdin) and has the following format:

T
n1 k1
a11 a12 ... a1n1
n2 k2
a21 a22 ... a2n2
...
nT kT
aT1 aT2 ... aTnT

Where:

  • T is the number of test cases.
  • For each test case, the first line contains two integers n and k.
  • The second line contains n space-separated integers representing the list elements.

outputFormat

For each test case, output the k-th smallest element on a new line via standard output (stdout).

## sample
3
5 2
7 10 4 3 20
6 5
5 2 1 9 4 6
5 3
1 2 3 4 5
4

6 3

</p>