Kth Smallest and Largest Element Problem Statement
You are provided with an array 'Arr' containing 'N' distinct integers and a positive integer 'K'. Your task is to find the Kth smallest and Kth largest elements of the array. It is assured that 'K' is not greater than the array's size.
Input:
The first line of input consists of an integer 'T' denoting the number of test cases.
The subsequent 2*T lines correspond to the 'T' test cases.
The first line of each test case contains two integers, 'N' and 'K'.
The second line of every test case contains 'N' integers representing the elements of array 'Arr'.
Output:
For each test case, output a line with two space-separated integers indicating the Kth smallest and Kth largest elements of the array.
Example:
Input:
4 3
1 2 5 4
Output:
4 2
Explanation:
For the given array [1, 2, 5, 4]
and K = 3, sorting the array gives [1, 2, 4, 5]
. Therefore, the 3rd smallest element is 4
and the 3rd largest element is 2
.
Constraints:
1 ≤ T ≤ 50
1 ≤ N ≤ 104
1 ≤ K ≤ N
-109 ≤ Arr[i] ≤ 109
- Time limit: 1 sec

AnswerBot
4mo
Find the Kth smallest and largest elements in an array.
Sort the array and return the Kth element for smallest and (N-K+1)th element for largest.
Use a priority queue to efficiently find the Kth smalles...read more
Help your peers!
Add answer anonymously...
Amazon Software Developer Intern interview questions & answers
A Software Developer Intern was asked 3mo agoQ. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. For a given array, how would you count the number of inversions?
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked 3mo agoQ1. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ2. For a given array, how would you count the number of inversions?
A Software Developer Intern was asked 4mo agoQ3. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

