#K76042. K-th Smallest Element Finder

    ID: 34554 Type: Default 1000ms 256MiB

K-th Smallest Element Finder

K-th Smallest Element Finder

In this problem, you are given an integer (N), a sequence of (N) integers, and an integer (K). Your task is to find the (K)th smallest element in the sequence using the Quickselect algorithm.

The Quickselect algorithm is a selection algorithm to find the (k)th smallest element in an unordered list. It is related to the Quicksort sorting algorithm. The algorithm works by partitioning the array (similar to Quicksort) and then recursing only in the part of the array that contains the (k)th smallest element.

Input Format: The input is read from standard input (stdin). The first line contains a single integer (N) representing the number of elements in the sequence. The second line contains (N) integers separated by spaces. The third line contains an integer (K) representing which smallest element to find.

Output Format: Output a single integer which is the (K)th smallest element in the sequence.

inputFormat

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

  1. An integer (N) - the number of elements in the sequence.
  2. A line containing (N) space-separated integers representing the sequence.
  3. An integer (K) - the rank of the smallest element to extract.

outputFormat

Output to standard output (stdout) a single integer representing the (K)th smallest element in the given sequence.## sample

6
7 10 4 3 20 15
3
7