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!
Select
Add answer anonymously...

Accenture Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked 2w agoQ. Which technology stack have you worked with?
An Associate Software Engineer was asked 3w agoQ. What is NLP?
An Associate Software Engineer was asked 3w agoQ. Explain the concepts of OOPs.

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked 2w agoQ1. Which technology stack have you worked with?
An Associate Software Engineer was asked 3w agoQ2. What is NLP?
An Associate Software Engineer was asked 4w agoQ3. Explain the concepts of OOPs.

Top HR questions asked in Accenture Associate Software Engineer

An Associate Software Engineer was asked 2w agoQ1. Have you ever encountered a project issue with a teammate who unexpectedly left,...read more
An Associate Software Engineer was asked 2w agoQ2. Can you share something about yourself that is not included in your resume?
An Associate Software Engineer was asked 1mo agoQ3. What challenges did you face as a team leader during the project?
Accenture Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits