Search⌘ K
AI Features

Kth Largest Element in an Array

Explore methods to identify the kth largest element in an integer array by understanding problem constraints and implementing solutions using heaps and sorting. This lesson guides you through developing a clear approach in C++ to solve common Top K element problems encountered in 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 main.cpp in the following coding playground:

C++
usercode > main.cpp
int FindKthLargest(vector<int>& nums, int k)
{
// Replace this placeholder return statement with your code
return -1;
}
Kth Largest Element in an Array