Problem title
Difficulty
Avg time to solve

Colour The Graph
Moderate
20 mins
Symmetric Tree
Easy
20 mins
Count Subsequences
Moderate
30 mins
Rotting Oranges
Moderate
20 mins
Count Ways
Easy
24 mins
Largest Number in Binary Tree
Moderate
36 mins
Maximum Size Rectangle Sub-matrix With All 1's
Hard
10 mins
Kth Smallest Element
Easy
15 mins
Terms Of AP
Easy
10 mins
Count Distinct Element in Every K Size Window
Easy
15 mins
19

Kth Smallest Element

Difficulty: EASY
Contributed By
Avg. time to solve
15 min
Success Rate
85%

Problem Statement

You are given an array of integers 'ARR' of size 'N' and another integer 'K'. Your task is to find and return 'K'th smallest value present in the array.

Note: All the elements in the array are distinct.

Input format
The first line of input contains an integer 'T' representing the number of the test case. Then the test case follows.

The first line of each test case contains two space-separated integers ‘N’ representing the size of the array and ‘K’.

The second line contains N space-separated integers that represent elements of the array ARR.
Output format
For each test case, print a single line that contains a single integer which is the Kth smallest element of the array.

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <=10 ^ 4
1 <= K <= N
1 <= ARR[i] <= 10 ^ 9

Time limit: 1 sec.
Sample Input 1:
3
6 3
1 2 3 4 5 6
5 4
1 3 8 6 2
1 1
5
Sample Output 1:
3
6
1
Explanation of Input 1:
(i) Sorted array will be 1 2 3 4 5 6, this shows that 3 is the third-smallest element in the array.
(ii) Sorted array will be 1 2 3 6 8, this shows that 6 is the fourth-smallest element in the array.
(iii) Sorted array will be 5, this shows that 5 is the smallest (and only) element in the array.
Sample Input 2:
3
2 2
1 14
2 1
1 14
3 2
6 7 9
Sample Output 2:
14
1
7
Explanation of Input 2:
(i) Sorted array will be 1 14, this shows that 14 is the second-smallest element in the array.
(ii) Sorted array will be 1 14, this shows that 1 is the smallest element in the array.
(iii) Sorted array will be 6 7 9, this shows that 7 is the second-smallest element in the array.    
Reset Code
Full screen
copy-code
Console