#K86197. K-th Smallest Element
K-th Smallest Element
K-th Smallest Element
Given an unsorted array of n integers and an integer k, your task is to determine the k-th smallest element in the array. Formally, if the array is sorted in non-decreasing order, you must output the element at position k (using 1-based indexing).
You are guaranteed that \(1 \leq k \leq n\). The array may contain duplicate elements.
Example: For the input array [7, 10, 4, 3, 20, 15]
with n = 6
and k = 3
, the sorted order is [3, 4, 7, 10, 15, 20]
and the answer is 7
.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains two integers,
n
andk
, separated by a space, wheren
is the number of elements in the array andk
is the position (1-indexed) of the element to find. - The second line contains
n
integers separated by spaces, representing the elements of the array.
outputFormat
Output a single integer to standard output (stdout) which is the k-th smallest element in the array.
## sample6 3
7 10 4 3 20 15
7
</p>