#C3548. K-th Smallest Element

    ID: 46987 Type: Default 1000ms 256MiB

K-th Smallest Element

K-th Smallest Element

You are given a list of n distinct integers and an integer k. Your task is to find the k-th smallest element in the list using the Quickselect algorithm. The Quickselect algorithm is an efficient selection algorithm to find the k-th smallest element in an unordered list. It is related to the Quicksort sorting algorithm.

In this problem, you must implement the Quickselect algorithm using the following key steps:

  • Partitioning: Rearrange elements in the array so that elements less than a chosen pivot are placed to its left and those greater are placed to its right.
  • Recursive Selection: Recursively apply the partition process to the part of the array that contains the k-th smallest element.

The desired element is defined as the one that would be at position k if the array were sorted in increasing order. In mathematical terms, if the sorted array is \(a_1, a_2, \dots, a_n\), you need to find \(a_k\).

inputFormat

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

n k
num1 num2 ... numN

Where:

  • n is the number of elements in the list.
  • k is the 1-based index of the smallest element to find.
  • num1, num2, ..., numN are the distinct integers of the list.

outputFormat

Output the k-th smallest element on a single line via standard output (stdout).

## sample
6 3
7 10 4 3 20 15
7