Last Updated: April 3, 2026
We have an array of integers and need to find the kth largest element. Not the kth distinct largest, just the kth from the top if you were to sort the array in descending order. So if the array has duplicates, they each count as separate positions.
For example, in [3,2,3,1,2,4,5,5,6], the sorted descending order is [6,5,5,4,3,3,2,2,1]. The 4th largest is 4, even though there are only 6 distinct values.
The problem also hints at solving it without sorting, which suggests there is a more efficient approach than the obvious O(n log n) sort. This is one of the most classic interview problems, and interviewers expect you to know multiple approaches with different trade-offs.
1 <= k <= nums.length <= 10^5 → n can be up to 100,000, so O(n^2) worst case is risky. We should aim for O(n log n) or better. An O(n) average-case solution would be ideal.-10^4 <= nums[i] <= 10^4 → Values range from -10,000 to 10,000. This limited range opens up counting-based approaches. The total range is only 20,001 distinct values.k <= nums.length → k is always valid, so we don't need to handle the case where k is larger than the array.The most straightforward way to find the kth largest element: sort the entire array, then just read off the answer. If the array is sorted in ascending order, the kth largest element sits at index n - k. If sorted in descending order, it is at index k - 1.
This is the approach most people think of first, and it is perfectly valid. It is easy to code, easy to verify, and with n up to 10^5, an O(n log n) sort finishes in milliseconds.
nums.length - k.We are sorting the entire array just to find one element. What if we only tracked the k largest elements we have seen so far?
Instead of sorting everything, maintain a collection of just the k largest elements seen so far. The smallest element in that collection is the answer: the kth largest overall.
A min-heap of size k is perfect for this. As you scan through the array, you add elements to the heap. Once the heap grows beyond size k, you remove the smallest element. After processing all elements, the heap contains exactly the k largest elements, and the root (the minimum) is the kth largest.
Think of it like a VIP list with k spots. Everyone tries to get in, but only the top k stay. The person at the bottom of the VIP list is the kth most important.
The min-heap acts as a sliding filter. At any point, it holds the k largest elements seen so far. When a new element arrives, it competes for a spot. If it is larger than the current kth largest (the heap root), it pushes that element out and takes its place. If it is smaller, it gets added and immediately removed. By the time we have processed all n elements, the heap contains exactly the k largest from the entire array.
The heap approach is O(n log k), which is great when k is small. But what if we could find the kth largest in O(n) average time by narrowing the search space with each step?
Quickselect is based on the same partition idea as quicksort, but with a key twist: instead of recursing into both halves, we only recurse into the half that contains the element we are looking for. This is the same idea behind binary search, applied to an unsorted array using partitioning.
Pick a pivot element and partition the array so everything less than the pivot is on the left, everything greater is on the right. After partitioning, the pivot is in its correct sorted position. If that position matches n - k, we are done. If the pivot ended up too far right, our answer is in the left partition. If too far left, it is in the right partition.
On average, each partition step eliminates about half the remaining elements. So instead of O(n log n) to sort everything, we do O(n) + O(n/2) + O(n/4) + ... = O(2n) = O(n) average work.
The partition step places the pivot in its final sorted position. Every element to the left is smaller, every element to the right is larger. So after one partition, we know exactly where the pivot would be in the fully sorted array, without actually sorting anything else. If the pivot lands at our target index, we are done. Otherwise, we know which half to search, and we can discard the other half entirely. The three-way partition (Dutch National Flag) handles duplicate values efficiently by settling the entire "equal" region in one pass.
nums.length - k (the position of the kth largest in a sorted array).