A heap is a special tree-based data structure that satisfies the heap property. There are two types of heaps:
Heaps are commonly used to implement priority queues, making them a versatile choice for finding the Kth largest or Kth smallest elements.
Given an array of integers and an integer K, our goal is to find:
To achieve this using heaps, we can utilize the following strategies:
For finding the Kth largest element, we can maintain a min heap of size K. By pushing all elements of the array into this min heap, we ensure that once we reach K elements, the root of the heap represents the Kth largest element.
For finding the Kth smallest element, we can employ a max heap of size K. In this case, the root will represent the Kth smallest element after processing the array.
Let's look at the implementation for finding the Kth largest element.
import java.util.PriorityQueue; public class KthLargest { public static int findKthLargest(int[] nums, int k) { // Create a min heap with the default constructor PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // Add elements to the min heap for (int num : nums) { minHeap.add(num); // Ensure the heap size does not exceed K if (minHeap.size() > k) { minHeap.poll(); // Removes the smallest (root) element } } return minHeap.peek(); // The root now is the Kth largest } public static void main(String[] args) { int[] nums = {3, 2, 1, 5, 6, 4}; int k = 2; // Looking for the 2nd largest element System.out.println("The " + k + "th largest element is: " + findKthLargest(nums, k)); } }
Explanation:
Now, let’s implement the solution for finding the Kth smallest element.
import java.util.Collections; import java.util.PriorityQueue; public class KthSmallest { public static int findKthSmallest(int[] nums, int k) { // Create a max heap using a custom comparator PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // Add elements to the max heap for (int num : nums) { maxHeap.add(num); // Ensure the heap size does not exceed K if (maxHeap.size() > k) { maxHeap.poll(); // Removes the largest (root) element } } return maxHeap.peek(); // The root now is the Kth smallest } public static void main(String[] args) { int[] nums = {3, 2, 1, 5, 6, 4}; int k = 2; // Looking for the 2nd smallest element System.out.println("The " + k + "th smallest element is: " + findKthSmallest(nums, k)); } }
Explanation:
Collections.reverseOrder()
comparator.Time Complexity: Both approaches have a time complexity of O(N log K), where N is the number of elements in the array. This is because each insertion into the heap takes O(log K) time, and we perform N insertions.
Space Complexity: The space complexity is O(K) due to the storage required for keeping K elements in the heap.
Using heaps to find the Kth largest and smallest elements in an array provides an efficient method that boils down the problem to managing a fixed-size heap. Whether using a min heap or a max heap, we can navigate through the array elements seamlessly and arrive at our required results promptly. Implementing these solutions in Java provides a practical insight into heap operations and enhances your understanding of advanced data structures.
15/11/2024 | DSA
16/11/2024 | DSA
23/09/2024 | DSA
16/11/2024 | DSA
13/10/2024 | DSA
13/10/2024 | DSA
13/10/2024 | DSA
15/11/2024 | DSA
08/12/2024 | DSA
16/11/2024 | DSA