Skip to content

Commit 95875b0

Browse files
Add another method to find kth number (#5918)
1 parent 871e4df commit 95875b0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/main/java/com/thealgorithms/maths/FindKthNumber.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.maths;
22

3+
import java.util.Collections;
4+
import java.util.PriorityQueue;
35
import java.util.Random;
46

57
/**
@@ -62,4 +64,19 @@ private static void swap(int[] array, int i, int j) {
6264
array[i] = array[j];
6365
array[j] = temp;
6466
}
67+
68+
public static int findKthMaxUsingHeap(int[] array, int k) {
69+
if (k <= 0 || k > array.length) {
70+
throw new IllegalArgumentException("k must be between 1 and the size of the array");
71+
}
72+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // using max-heap to store numbers.
73+
for (int num : array) {
74+
maxHeap.add(num);
75+
}
76+
while (k > 1) {
77+
maxHeap.poll(); // removing max number from heap
78+
k--;
79+
}
80+
return maxHeap.peek();
81+
}
6582
}

src/test/java/com/thealgorithms/maths/FindKthNumberTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,50 @@ public void testFindKthMaxTypicalCases() {
1414
assertEquals(3, FindKthNumber.findKthMax(array1, 3));
1515
assertEquals(4, FindKthNumber.findKthMax(array1, 2));
1616
assertEquals(5, FindKthNumber.findKthMax(array1, 1));
17+
assertEquals(3, FindKthNumber.findKthMaxUsingHeap(array1, 3));
18+
assertEquals(4, FindKthNumber.findKthMaxUsingHeap(array1, 2));
19+
assertEquals(5, FindKthNumber.findKthMaxUsingHeap(array1, 1));
1720

1821
int[] array2 = {7, 5, 8, 2, 1, 6};
1922
assertEquals(5, FindKthNumber.findKthMax(array2, 4));
2023
assertEquals(6, FindKthNumber.findKthMax(array2, 3));
2124
assertEquals(8, FindKthNumber.findKthMax(array2, 1));
25+
assertEquals(5, FindKthNumber.findKthMaxUsingHeap(array2, 4));
26+
assertEquals(6, FindKthNumber.findKthMaxUsingHeap(array2, 3));
27+
assertEquals(8, FindKthNumber.findKthMaxUsingHeap(array2, 1));
2228
}
2329

2430
@Test
2531
public void testFindKthMaxEdgeCases() {
2632
int[] array1 = {1};
2733
assertEquals(1, FindKthNumber.findKthMax(array1, 1));
34+
assertEquals(1, FindKthNumber.findKthMaxUsingHeap(array1, 1));
2835

2936
int[] array2 = {5, 3};
3037
assertEquals(5, FindKthNumber.findKthMax(array2, 1));
3138
assertEquals(3, FindKthNumber.findKthMax(array2, 2));
39+
assertEquals(5, FindKthNumber.findKthMaxUsingHeap(array2, 1));
40+
assertEquals(3, FindKthNumber.findKthMaxUsingHeap(array2, 2));
3241
}
3342

3443
@Test
3544
public void testFindKthMaxInvalidK() {
3645
int[] array = {1, 2, 3, 4, 5};
3746
assertThrows(IllegalArgumentException.class, () -> FindKthNumber.findKthMax(array, 0));
3847
assertThrows(IllegalArgumentException.class, () -> FindKthNumber.findKthMax(array, 6));
48+
assertThrows(IllegalArgumentException.class, () -> FindKthNumber.findKthMaxUsingHeap(array, 0));
49+
assertThrows(IllegalArgumentException.class, () -> FindKthNumber.findKthMaxUsingHeap(array, 6));
3950
}
4051

4152
@Test
4253
public void testFindKthMaxLargeArray() {
4354
int[] array = generateArray(1000);
4455
int k = new Random().nextInt(1, array.length);
4556
int result = FindKthNumber.findKthMax(array, k);
57+
int maxK = FindKthNumber.findKthMaxUsingHeap(array, k);
4658
Arrays.sort(array);
4759
assertEquals(array[array.length - k], result);
60+
assertEquals(array[array.length - k], maxK);
4861
}
4962

5063
public static int[] generateArray(int capacity) {

0 commit comments

Comments
 (0)