AlgoMaster Logo

Kth Largest Element in an Array

Last Updated: April 3, 2026

medium
4 min read

Understanding the Problem

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.

Key Constraints:

  • 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.

Approach 1: Sorting

Intuition

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.

Algorithm

  1. Sort the array in ascending order.
  2. Return the element at index nums.length - k.

Example Walkthrough

1Initial array: nums = [3,2,1,5,6,4], k = 2
0
3
1
2
2
1
3
5
4
6
5
4
1/4

Code

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?

Approach 2: Min-Heap

Intuition

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.

Algorithm

  1. Create a min-heap (priority queue).
  2. Iterate through each number in the array.
  3. Add the number to the heap.
  4. If the heap size exceeds k, remove the smallest element (the root).
  5. After processing all elements, the root of the heap is the kth largest element.

Example Walkthrough

1Start: process nums[0]=3, add to heap
0
3
i
1
2
2
1
3
5
4
6
5
4
1/7
1Add 3, heap size=1 <= k=2
3
1/7

Code

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?

Approach 3: Quickselect

Intuition

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.

Algorithm

  1. Set the target index to nums.length - k (the position of the kth largest in a sorted array).
  2. Choose a random pivot from the current search range.
  3. Partition the array: elements less than pivot go left, equal elements go middle, greater go right.
  4. If the target index falls in the "equal" section, return the pivot.
  5. If the target index is in the left section, narrow the search to the left portion.
  6. If the target index is in the right section, narrow the search to the right portion.
  7. Repeat until found.

Example Walkthrough

1Initial: target index = n - k = 6 - 2 = 4, search [0..5]
0
3
1
2
2
1
3
5
4
6
5
4
search range
1/6

Code