


Ninja loves playing with numbers. One day Alice gives him some numbers and asks him to find the Kth largest value among them.
The first line of input contains an integer ‘T,’ denoting the number of test cases. The test cases follow.
The first line of each test case contains two space-separated integers, ‘N’ and ‘K’, denoting the number of elements in the array and the index of the largest number to be found respectively.
Output Format:
For each test case, print an integer denoting the Kth largest number.
Print the output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1<= T <= 50
1 <= K <= N <= 10^4
1 <= array[i] <= 10^5
Time Limit: 1 sec
2
5 2
1 2 2 3 4
5 3
10 11 23 23 23
3
23
In the first test case, out of the given elements, the first largest is 4, and the second largest is 3. So we return 3 as our answer.
In the second test case, out of the given elements, the first largest is 23, the second largest is 23, and the third-largest is 23. So we return 23 as our answer.
2
7 2
1 2 3 4 5 6 7
8 4
3 4 6 9 5 10 11 12
6
9
Can you sort the elements.
The idea is to sort the elements and return the kth largest element among them.
The steps are as follows:
O(N*logN), where ‘N’ is the total number of elements in the given array.
As Sorting takes N*LogN complexity, so the overall complexity is O(N*logN).
O(1), no extra space required.
As we are not using any extra space. Hence, the overall space complexity is O(1).