Skip to content

Commit d572997

Browse files
authored
Add KthElementFinder algorithm (#5836)
1 parent cdf509f commit d572997

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
* [GenericHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/GenericHeap.java)
175175
* [Heap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/Heap.java)
176176
* [HeapElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/HeapElement.java)
177+
* [KthElementFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/KthElementFinder.java)
177178
* [LeftistHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java)
178179
* [MaxHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java)
179180
* [MinHeap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java)
@@ -848,6 +849,7 @@
848849
* heaps
849850
* [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
850851
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
852+
* [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
851853
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
852854
* [MinPriorityQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java)
853855
* lists
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
package com.thealgorithms.datastructures.heaps;
3+
4+
import java.util.PriorityQueue;
5+
6+
/**
7+
* This class provides methods to find the Kth largest or Kth smallest element
8+
* in an array using heaps. It leverages a min-heap to find the Kth largest element
9+
* and a max-heap to find the Kth smallest element efficiently.
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class KthElementFinder {
14+
private KthElementFinder() {
15+
}
16+
17+
/**
18+
* Finds the Kth largest element in the given array.
19+
* Uses a min-heap of size K to track the largest K elements.
20+
*
21+
* Time Complexity: O(n * log(k)), where n is the size of the input array.
22+
* Space Complexity: O(k), as we maintain a heap of size K.
23+
*
24+
* @param nums the input array of integers
25+
* @param k the desired Kth position (1-indexed, i.e., 1 means the largest element)
26+
* @return the Kth largest element in the array
27+
*/
28+
public static int findKthLargest(int[] nums, int k) {
29+
PriorityQueue<Integer> minHeap = new PriorityQueue<>(k);
30+
for (int num : nums) {
31+
minHeap.offer(num);
32+
if (minHeap.size() > k) {
33+
minHeap.poll();
34+
}
35+
}
36+
return minHeap.peek();
37+
}
38+
39+
/**
40+
* Finds the Kth smallest element in the given array.
41+
* Uses a max-heap of size K to track the smallest K elements.
42+
*
43+
* Time Complexity: O(n * log(k)), where n is the size of the input array.
44+
* Space Complexity: O(k), as we maintain a heap of size K.
45+
*
46+
* @param nums the input array of integers
47+
* @param k the desired Kth position (1-indexed, i.e., 1 means the smallest element)
48+
* @return the Kth smallest element in the array
49+
*/
50+
public static int findKthSmallest(int[] nums, int k) {
51+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
52+
for (int num : nums) {
53+
maxHeap.offer(num);
54+
if (maxHeap.size() > k) {
55+
maxHeap.poll();
56+
}
57+
}
58+
return maxHeap.peek();
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.thealgorithms.datastructures.heaps;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class KthElementFinderTest {
8+
@Test
9+
public void testFindKthLargest() {
10+
int[] nums = {3, 2, 1, 5, 6, 4};
11+
assertEquals(5, KthElementFinder.findKthLargest(nums, 2));
12+
}
13+
14+
@Test
15+
public void testFindKthSmallest() {
16+
int[] nums = {7, 10, 4, 3, 20, 15};
17+
assertEquals(7, KthElementFinder.findKthSmallest(nums, 3));
18+
}
19+
}

0 commit comments

Comments
 (0)