Kth Largest Element in an Array Solution
Given an integer array nums and an integer k, return the kth largest element. Note: it is the kth largest in sorted order, not the kth distinct element.
Problem Statement
Given an integer array nums and an integer k, return the kth largest element in the array. You must solve it in O(n) average time.
Sort โ O(n log n)
Sort the array in descending order. The answer is at index k - 1. Correct and simple, but doesn't meet the O(n) average requirement.
Sorting places all elements in order. The kth largest is directly accessible by index. No element is missed.
| Metric | Value |
|---|---|
| Time | O(n log n) |
| Space | O(1) โ in-place sort (or O(n) for Python's timsort) |
Min-Heap of Size k โ O(n log k)
Maintain a min-heap of at most k elements. Iterate through the array โ if the heap has fewer than k elements, add the current one; otherwise, add it and poll the smallest. After processing all elements, the root is the kth largest.
- Step 1: Create an empty min-heap.
- Step 2: For each element in nums: offer it to the heap.
- Step 3: If heap size exceeds k, poll the root (smallest).
- Step 4: After all elements, return
heap.peek().
- "Find the kth largest/smallest" in a static array โ think min-heap of size k for the clean O(n log k) solution, or quickselect for the interview-flashy O(n) average solution.
- This appears in LC 215, LC 347 (Top K Frequent), LC 973 (K Closest Points), and LC 703 (Kth Largest in Stream).
- Based on quicksort's partition. Pick a pivot, partition so elements โฅ pivot are on the left.
- If pivot lands at index k-1, it's the answer. Otherwise recurse on the side containing the target.
- O(n) average, O(nยฒ) worst-case. Randomized pivot avoids worst-case in practice.
Visual Walkthrough
Trace min-heap approach with nums = [3, 2, 1, 5, 6, 4], k = 2:
- The heap never grows beyond k=2 elements. After processing all 6 elements, only the top-2 (5 and 6) remain.
- The root (5) is the 2nd largest. Total work: 6 offers + 4 polls = O(n log k).
Code โ Java & Python
Complexity Analysis
| Approach | Time | Space | Trade-off |
|---|---|---|---|
| Sort | O(n log n) | O(1) | Simple; overshoots by sorting everything |
| Min-Heap size k | O(n log k) | O(k) | Clean, predictable. Best when k โช n. |
| Quickselect โ optimal | O(n) avg | O(1) | O(nยฒ) worst-case; randomize pivot to avoid. |
- The heap approach is simpler and has a guaranteed O(n log k) bound. Quickselect is O(n) average but O(nยฒ) worst-case.
- In interviews, most interviewers accept the heap solution. Mention quickselect as a follow-up to show depth.
- For production code,
std::nth_element(C++) or randomized quickselect is preferred.
Edge Cases & Pitfalls
| Case | Input | Why It Matters |
|---|---|---|
| k = 1 (maximum) | [3,1,2], k=1 | Heap of size 1 โ just track the max. Returns 3. |
| k = n (minimum) | [3,1,2], k=3 | Heap holds all elements. Root = minimum = 1. |
| All duplicates | [5,5,5], k=2 | Heap holds [5,5]. Peek = 5. Duplicates are counted individually. |
| Negative numbers | [-1,-2,-3], k=1 | Largest is -1. Heap handles negatives correctly. |
| Single element | [42], k=1 | Only one element โ return it. |
| Already sorted descending | [5,4,3,2,1], k=3 | First k elements fill heap, rest get polled. Answer: 3. |