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.
AnswerBot
4mo

Find the Kth largest element in an array of distinct positive integers.

  • Sort the array in non-increasing order and return the Kth element.

  • Use a priority queue or quick select algorithm for efficient so...read more

Prathamesh Bhanuse
2mo
array=[34,76,46,13,42,95] n=4 def array_nth(array): array.sort(reverse=True) return array[n-1] print(array_nth(array))
Help your peers!
Select
Add answer anonymously...

Bounteous x Accolite Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Ninja and the Maze Problem Statement Ninja is stuck in a maze represented as a 2...read more
A Software Developer Intern was asked Q. Median of Two Sorted Arrays Given two sorted arrays A and B of sizes N and M, fi...read more
A Software Developer Intern was asked Q. Distance Between Two Nodes in a Binary Tree Given a binary tree and the values o...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Ninja and the Maze Problem Statement Ninja is stuck in a maze represented as a 2...read more
A Software Developer Intern was asked Q2. Median of Two Sorted Arrays Given two sorted arrays A and B of sizes N and M, fi...read more
A Software Developer Intern was asked Q3. Distance Between Two Nodes in a Binary Tree Given a binary tree and the values o...read more
Bounteous x Accolite Software Developer Intern 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