Search⌘ K
AI Features

Kth Largest Element in an Array

Understand how to solve the problem of finding the kth largest element in an integer array by applying the top k elements pattern. Learn to use heaps for efficient selection and practice implementing your solution to enhance problem-solving skills for coding interviews.

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.

Python
usercode > main.py
def find_kth_largest(nums, k):
# Replace this placeholder return statement with your code
return -1
Kth Largest Element in an Array