Skip to content

Added another method to find kth number #5918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2024
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
17 changes: 17 additions & 0 deletions src/main/java/com/thealgorithms/maths/FindKthNumber.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thealgorithms.maths;

import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Random;

/**
Expand Down Expand Up @@ -62,4 +64,19 @@ private static void swap(int[] array, int i, int j) {
array[i] = array[j];
array[j] = temp;
}

public static int findKthMaxUsingHeap(int[] array, int k) {
if (k <= 0 || k > array.length) {
throw new IllegalArgumentException("k must be between 1 and the size of the array");
}
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // using max-heap to store numbers.
for (int num : array) {
maxHeap.add(num);
}
while (k > 1) {
maxHeap.poll(); // removing max number from heap
k--;
}
return maxHeap.peek();
}
}
13 changes: 13 additions & 0 deletions src/test/java/com/thealgorithms/maths/FindKthNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,50 @@ public void testFindKthMaxTypicalCases() {
assertEquals(3, FindKthNumber.findKthMax(array1, 3));
assertEquals(4, FindKthNumber.findKthMax(array1, 2));
assertEquals(5, FindKthNumber.findKthMax(array1, 1));
assertEquals(3, FindKthNumber.findKthMaxUsingHeap(array1, 3));
assertEquals(4, FindKthNumber.findKthMaxUsingHeap(array1, 2));
assertEquals(5, FindKthNumber.findKthMaxUsingHeap(array1, 1));

int[] array2 = {7, 5, 8, 2, 1, 6};
assertEquals(5, FindKthNumber.findKthMax(array2, 4));
assertEquals(6, FindKthNumber.findKthMax(array2, 3));
assertEquals(8, FindKthNumber.findKthMax(array2, 1));
assertEquals(5, FindKthNumber.findKthMaxUsingHeap(array2, 4));
assertEquals(6, FindKthNumber.findKthMaxUsingHeap(array2, 3));
assertEquals(8, FindKthNumber.findKthMaxUsingHeap(array2, 1));
}

@Test
public void testFindKthMaxEdgeCases() {
int[] array1 = {1};
assertEquals(1, FindKthNumber.findKthMax(array1, 1));
assertEquals(1, FindKthNumber.findKthMaxUsingHeap(array1, 1));

int[] array2 = {5, 3};
assertEquals(5, FindKthNumber.findKthMax(array2, 1));
assertEquals(3, FindKthNumber.findKthMax(array2, 2));
assertEquals(5, FindKthNumber.findKthMaxUsingHeap(array2, 1));
assertEquals(3, FindKthNumber.findKthMaxUsingHeap(array2, 2));
}

@Test
public void testFindKthMaxInvalidK() {
int[] array = {1, 2, 3, 4, 5};
assertThrows(IllegalArgumentException.class, () -> FindKthNumber.findKthMax(array, 0));
assertThrows(IllegalArgumentException.class, () -> FindKthNumber.findKthMax(array, 6));
assertThrows(IllegalArgumentException.class, () -> FindKthNumber.findKthMaxUsingHeap(array, 0));
assertThrows(IllegalArgumentException.class, () -> FindKthNumber.findKthMaxUsingHeap(array, 6));
}

@Test
public void testFindKthMaxLargeArray() {
int[] array = generateArray(1000);
int k = new Random().nextInt(1, array.length);
int result = FindKthNumber.findKthMax(array, k);
int maxK = FindKthNumber.findKthMaxUsingHeap(array, k);
Arrays.sort(array);
assertEquals(array[array.length - k], result);
assertEquals(array[array.length - k], maxK);
}

public static int[] generateArray(int capacity) {
Expand Down