
Asked in Amazon
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...
Top Software Developer Intern Interview Questions Asked at Amazon
Q. What is the code to calculate the distance between two nodes in a binary tree?
Q. For a given array, how would you count the number of inversions?
Q. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
Interview Questions Asked to Software Developer Intern at Other Companies
Top Skill-Based Questions for Amazon Software Developer Intern
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Operating Systems Interview Questions and Answers
250 Questions
System Design Interview Questions and Answers
250 Questions
C++ Interview Questions and Answers
150 Questions
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

