Problem of the day
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.
If 'N' is 5 and 'K' is 3 and the array is 7, 2, 6, 1, 9
Sorting the array we get 1, 2, 6, 7, 9
Hence the 3rd smallest number is 6.
The first line 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
Print a single line that contains a single integer which is the 'Kth' smallest element of the array.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
7 5
5 9 1 8 10 6 4
8
Sorted array will be 1 4 5 6 8 9 10, this shows that 8 is the fifth-smallest element in the array.
7 2
24 8 23 28 3 1 19
3
1 <= N <=10 ^ 4
1 <= K <= N
1 <= ARR[i] <= 10 ^ 9
Time limit: 1 sec.
Try to think of a way where you can change elements of ARR in such a way that you are able to get Kth smallest element directly in O(1)
O(N * logN) where ‘N’ is the number of elements in the array.
As we are sorting the array which takes O(N * log(N)) time.
O(1).
Since we are not using any extra space.