Kth Largest Element Problem

Given an array containing N distinct positive integers and a number K, determine the Kth largest element in the array.

Example:

Input:
N = 6, K = 3, array = [2, 1, 5, 6, 3, 8]
Output:
5
Explanation:

Sorting the array in non-increasing order gives [8, 6, 5, 3, 2, 1]. The 3rd largest element is 5.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 10^4
  • 1 <= ARR[i] <= 10^9
  • 1 <= K <= N
Note:
Kth largest element in an array is the kth element when sorted in descending order. All elements are distinct.

Input:

The first line contains an integer 'T' representing the number of test cases. Each test case consists of two lines. The first line contains integers 'N' and 'K'. The second line contains 'N' space-separated integers of the array.

Output:

Print the Kth largest element for each test case.
2K20_SE_161_Vishal Roy Roy
1y

if we think about this question, then the first approach that comes into our mind should sort the array and then pick the kth element from last, but its time complexity would be high, can optimize it ...read more

Help your peers!
Add answer anonymously...
PayPal Software Developer 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