Daily LeetCode Problems: Problem 215. Kth Largest Element in an Array

Finding the Kth Largest Element: Unveiling LeetCode Problem 215

Monit Sharma
3 min readAug 14, 2023

Introduction:

Welcome to another insightful problem-solving article! Today, we’re delving into LeetCode problem 215, titled “Kth Largest Element in an Array.” This intriguing problem challenges us to find the kth largest element in a given integer array. We’ll dive into the problem statement, analyze the requirements, explore an efficient approach to solve it without sorting, provide pseudocode, discuss the underlying techniques, and wrap up with complexity analysis.

Problem Statement:

The problem presents us with an array of integers, nums, and an integer value, k. Our goal is to find the kth largest element in the array. It's important to note that the problem asks for the kth largest element in the sorted order, not the kth distinct element. Furthermore, the problem encourages us to explore a solution that doesn't involve sorting the entire array.

Approach:

To solve this problem efficiently without sorting the entire array, we can utilize the concept of a min-heap (also known as a priority queue). We’ll create a min-heap and maintain its size to be k. As we iterate through the elements of the array, we’ll add each element to the min-heap. If the size of the heap exceeds k, we’ll remove the smallest element from the heap. At the end, the top element of the min-heap will be the kth largest element.

Pseudocode:

class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
min_heap = []

for num in nums:
heapq.heappush(min_heap, num)
if len(min_heap) > k:
heapq.heappop(min_heap)

return min_heap[0]

Explanation:

  1. Create an empty min-heap.
  2. Iterate through each element in the array.
  3. Add the element to the min-heap using heapq.heappush().
  4. If the size of the min-heap becomes greater than k, remove the smallest element using heapq.heappop().
  5. The top element of the min-heap will be the kth largest element.

Complexity Analysis:

  • Time complexity: We perform n heapq.heappush() operations, each of which takes O(log k) time due to maintaining the heap property. Additionally, we perform at most n - k heapq.heappop() operations, each of which also takes O(log k) time. Therefore, the overall time complexity is O(n log k).
  • Space complexity: The min-heap stores at most k elements, resulting in a space complexity of O(k).

Full Solution

Python

class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
minHeap = []

for num in nums:
heapq.heappush(minHeap, num)
if len(minHeap) > k:
heapq.heappop(minHeap)

return minHeap[0]

Java

class Solution {
public int findKthLargest(int[] nums, int k) {
Queue<Integer> minHeap = new PriorityQueue<>();

for (final int num : nums) {
minHeap.offer(num);
while (minHeap.size() > k)
minHeap.poll();
}

return minHeap.peek();
}
}

C++

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
multiset<int> min_heap; //min heap implemetation
for (int num : nums){
min_heap.insert(num);
if(min_heap.size() > k) min_heap.erase(min_heap.begin());

}

return *min_heap.begin();
}
};

Conclusion:

In this article, we explored LeetCode problem 215, “Kth Largest Element in an Array.” We delved into the problem statement, discussed an efficient approach using a min-heap, provided pseudocode for implementation, and analyzed the time and space complexities. By utilizing a min-heap to maintain the kth largest elements, we successfully solved the problem without sorting the entire array. Keep practicing such problems to enhance your algorithmic skills. Stay tuned for more captivating LeetCode challenges in our daily series. Happy coding!

--

--