
Asked in Accenture
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.
Ensure K is within the array size to avoid out of bounds...read more
Help your peers!
Add answer anonymously...
Top Associate Software Engineer Interview Questions Asked at Accenture
Q. Which technology stack have you worked with?
Q. What is NLP?
Q. Explain the concepts of OOPs.
Interview Questions Asked to Associate Software Engineer at Other Companies
Top Skill-Based Questions for Accenture Associate Software Engineer
Data Structures Interview Questions and Answers
250 Questions
Algorithms Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Web Development 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

