#C14915. K-th Smallest Element
K-th Smallest Element
K-th Smallest Element
You are given an unsorted array of n integers and an integer k. Your task is to find the k-th smallest element in the array. Formally, if the array after sorting in non-decreasing order is \(a_1, a_2, \ldots, a_n\), you need to output \(a_k\).
The problem tests your ability to implement efficient sorting or selection algorithms. Ensure that your solution reads from stdin
and writes the result to stdout
.
inputFormat
The first line contains two integers (n) and (k), where (n) is the number of elements in the array and (k) is the position of the smallest element to find (1-based index). The second line contains (n) space-separated integers representing the array elements.
outputFormat
Output a single integer representing the k-th smallest element in the array.## sample
6 3
7 10 4 3 20 15
7