ExamAdda Logo

Kth Largest Element in an Array

Easy

Given an integer array nums and an integer k, return the kth largest element in the array.

The kth largest element is the element that would appear at position k when the array is sorted in descending order.

The array may contain duplicate values.

You should solve the problem without requiring the entire array to be sorted.

Example 1

Input

n = 6
nums = [3, 2, 1, 5, 6, 4]
k = 2

Output

5

Explanation

Sorted in descending order:

[6, 5, 4, 3, 2, 1]

 

The 2nd largest element is 5.

Example 2

Input

n = 9
nums = [3, 2, 3, 1, 2, 4, 5, 5, 6]
k = 4

Output

4

Explanation

Sorted in descending order:

[6, 5, 5, 4, 3, 3, 2, 2, 1]

 

The 4th largest element is 4.

Constraints

1 <= nums.length <= 10^5
1 <= k <= nums.length
-10^4 <= nums[i] <= 10^4

Hints:

Hint 1

A min-heap of size k can keep track of the k largest elements seen so far.

Hint 2

After processing all elements, the root of the min-heap is the kth largest element.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 22, 2026

Last updated

Aug 22, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output