Given an integer array nums and an integer k, return the kth largest element in the array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
Can you solve it without sorting?
We use analytics and advertising cookies to understand how the site is used and whether our ads on Facebook and Instagram work. They are set only if you accept. See our Privacy Policy for details.
Given an integer array nums and an integer k, return the kth largest element in the array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
Can you solve it without sorting?
A medium heap / priority queue problem, graded against 6 test cases (3 of them hidden).
A priority queue for "top k" and streaming-median questions.
Reach for it when you see: "Top/smallest k", a running median, or repeatedly needing the current minimum.
More Heap / Priority Queue problems →Maintain a min-heap of size k. The root is the kth largest.
Solution 2: Quickselect
Partition around a pivot until we find the kth largest position.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.