Kth Smallest Element
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.
CodingNinjas
author
2y
Max Heaps
We’ll use a max heap of fixed size 'K' to store the elements of 'ARR' and perform a single 'GETMAX' operation to get 'K'th minimum element.
- We’ll create an empty heap 'MAXHEAP'.
- We’ll add 'ARR'...read more
CodingNinjas
author
2y
Brute Force
- Sort the elements of ‘ARR’ using function ‘SORT’
- Return element at ('K' - 1)th index
O(1).
Since we are not using any extra space.
Time Complexity: O(nlogn)E...read moreCodingNinjas
author
2y
Min Heaps
- We’ll use min heaps to store the elements of 'ARR' and perform 'K' GETMIN operations to get Kth minimum element.
- We’ll create an empty heap MINHEAP.
- We’ll add 'ARR'['I'] to MINHEAP for each 0 <...read more
Add answer anonymously...
Top Compass Group Support Services Software Engineer interview questions & answers
Popular interview questions of Software Engineer
>
Compass Group Support Services Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app