#C14048. Find the k-th Largest Element
Find the k-th Largest Element
Find the k-th Largest Element
You are given an unsorted array of integers and a positive integer (k). Your task is to find the (k)-th largest element in the array. In other words, if the array were sorted in descending order, you need to return the element at the (k)-th position.
For example, given the array [3, 2, 1, 5, 6, 4] and (k = 2), the output should be 5 because 5 is the second largest element.
An efficient way to solve this problem is by using the QuickSelect algorithm, which has an average-case time complexity of (O(n)). Note that the algorithm involves partitioning the array around a random pivot and recursively selecting the (k)-th largest element in the appropriate subarray.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains two integers (n) and (k), where (n) represents the number of elements in the array and (k) represents the (k)-th position (1-indexed).
- The second line contains (n) space-separated integers representing the elements of the array.
outputFormat
The output is a single integer written to standard output (stdout), which is the (k)-th largest element in the given array.## sample
1 1
1
1