Kth Largest Element

Problem Overview
The problem "Kth Largest Element in an Array" is a well-known question that appears frequently in technical interviews and competitive programming contests. It involves finding the ( k^{th} ) largest element in an unsorted array. This problem effectively tests one's understanding of heaps and partition-based algorithms like Quickselect.
You are given an integer array nums
and an integer k
, and you need to return the ( k^{th} ) largest element in the array.
Example:
Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
Understanding the Problem
The task is simple—find the element that would appear in the k-th position if the array were sorted in decreasing order. However, directly sorting the array isn't the most efficient solution in terms of time complexity, especially if we need the solution to be optimal.
Key DSA Concepts and Theory
1. Heap
The most efficient way to solve this problem is by utilizing a data structure known as a Heap. Specifically, a Min-Heap or a Max-Heap can be used.
- Min-Heap: A binary tree where the parent's node value is less than or equal to the children. The smallest element is at the root.
- Max-Heap: A binary tree where the parent's node value is greater than or equal to the children. The largest element is at the root.
For this problem:
- Min-Heap is used when we want to keep track of the largest
k
elements seen so far.
2. Quickselect Algorithm
Another efficient approach is using the Quickselect algorithm. This algorithm is a modification of the QuickSort algorithm. Instead of recursing into both halves of the partitioned array, Quickselect only recurses into one side—specifically the side that contains the ( k^{th} ) largest element.
Solution Approach
Using a Min-Heap
In this method, we'll maintain a min-heap with a size fixed to k
. As we iterate through the array, we add each element to the heap. If the heap exceeds k
elements, we remove the smallest element. This means that by the end, the heap will contain the k
largest elements of the array, and the root of the heap will be the ( k^{th} ) largest element.
C++ Code
#include <vector>
#include <queue>
using namespace std;
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> minHeap;
for (int num : nums) {
minHeap.push(num);
if (minHeap.size() > k) {
minHeap.pop();
}
}
return minHeap.top();
}
Java Code
import java.util.PriorityQueue;
public class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int num : nums) {
minHeap.add(num);
if (minHeap.size() > k) {
minHeap.poll();
}
}
return minHeap.peek();
}
}
Using Quickselect
Quickselect is another popular solution for this problem. It involves partitioning the array similar to a quicksort and only focusing on one half at a time until the partition is the kth largest element.
Time and Space Complexity Analysis
Min-Heap Solution
- Time Complexity: ( O(n \log k) ), where ( n ) is the number of elements in the array. This is because each insertion and deletion operation in the heap takes ( O(\log k) ) time.
- Space Complexity: ( O(k) ), which is the space required to store the heap.
Quickselect Solution
- Time Complexity: ( O(n) ) on average, but could go up to ( O(n^2) ) in the worst-case scenario.
- Space Complexity: ( O(1) ), since it is an in-place selection technique.
Common Mistakes to Avoid
- Not updating the heap correctly: Remember to always remove the smallest element from the Min-Heap once it exceeds size
k
. - Misunderstanding the ( k^{th} ) largest element's position. Ensure that the concepts of min-heap and k-th largest indexing are clear.
Similar Problems on LeetCode
- Problem 703: Kth Largest Element in a Stream
- Problem 215: Kth Largest Element in an Array
- Problem 347: Top K Frequent Elements
Additional Resources and References
- Introduction to Heaps and Priority Queues
- Quickselect Algorithm Explanation
- Practice problems on platforms like GeeksForGeeks and LeetCode.
By understanding both the Heap and Quickselect approaches, you are better equipped to tackle not just this problem, but a variety of selection problems that you may encounter in interviews or coding competitions.