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
2d

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...
Accenture Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter