Search⌘ K
AI Features

Kth Largest Element in an Array

Explore how to determine the kth largest element in an array by understanding its sorted order and applying heap techniques. Learn to solve this common coding interview problem with clear constraints and examples, building a reliable approach for similar top K element challenges.

Statement

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

Note: The kth largest element is defined with respect to the array’s sorted order (descending), and does not necessarily correspond to the kth unique value.

Constraints:

  • 11 \leq k \leq nums.length 103\leq 10^3

  • 104-10^4 \leq nums[i] 104\leq 10^4

Examples

canvasAnimation-image
1 / 4

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Kth Largest Element in an Array

1.

What is the 4th largest element in the following unsorted array?

[5, 12, 9, 0, 6, 7, 1, 8, 4, 9]

A.

9

B.

7

C.

8

D.

6


1 / 4

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in the following coding playground:

Java
usercode > KthLargestElement.java
import java.util.*;
public class KthLargestElement{
public static int findKthLargest(int[] nums, int k) {
// Replace this placeholder return statement with your code
return -1;
}
}
Kth Largest Element in an Array