Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
* [GenericHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java)
* [Heap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/Heap.java)
* [HeapElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/HeapElement.java)
* [KthElementFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/KthElementFinder.java)
* [LeftistHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java)
* [MaxHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java)
* [MinHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java)
Expand Down Expand Up @@ -848,6 +849,7 @@
* heaps
* [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
* [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
* [MinPriorityQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java)
* lists
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

package com.thealgorithms.datastructures.heaps;

import java.util.PriorityQueue;

/**
* This class provides methods to find the Kth largest or Kth smallest element
* in an array using heaps. It leverages a min-heap to find the Kth largest element
* and a max-heap to find the Kth smallest element efficiently.
*
* @author Hardvan
*/
public final class KthElementFinder {
private KthElementFinder() {
}

/**
* Finds the Kth largest element in the given array.
* Uses a min-heap of size K to track the largest K elements.
*
* Time Complexity: O(n * log(k)), where n is the size of the input array.
* Space Complexity: O(k), as we maintain a heap of size K.
*
* @param nums the input array of integers
* @param k the desired Kth position (1-indexed, i.e., 1 means the largest element)
* @return the Kth largest element in the array
*/
public static int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>(k);
for (int num : nums) {
minHeap.offer(num);
if (minHeap.size() > k) {
minHeap.poll();
}
}
return minHeap.peek();
}

/**
* Finds the Kth smallest element in the given array.
* Uses a max-heap of size K to track the smallest K elements.
*
* Time Complexity: O(n * log(k)), where n is the size of the input array.
* Space Complexity: O(k), as we maintain a heap of size K.
*
* @param nums the input array of integers
* @param k the desired Kth position (1-indexed, i.e., 1 means the smallest element)
* @return the Kth smallest element in the array
*/
public static int findKthSmallest(int[] nums, int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
for (int num : nums) {
maxHeap.offer(num);
if (maxHeap.size() > k) {
maxHeap.poll();
}
}
return maxHeap.peek();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.thealgorithms.datastructures.heaps;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class KthElementFinderTest {
@Test
public void testFindKthLargest() {
int[] nums = {3, 2, 1, 5, 6, 4};
assertEquals(5, KthElementFinder.findKthLargest(nums, 2));
}

@Test
public void testFindKthSmallest() {
int[] nums = {7, 10, 4, 3, 20, 15};
assertEquals(7, KthElementFinder.findKthSmallest(nums, 3));
}
}