Repository: TheAlgorithms/Java
Pull request #5918 (2024-10-20 17:47:12)

Added another method to find kth number
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->
Added another method to find kth number using max-heap(Priority Queue). Also added test cases.
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5918?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 66.80%. Comparing base [(`b190cb7`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b190cb72def8d926e2f73d9f80cfd27c88ff3a3a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`7fc4668`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7fc4668359388e0d63e21eea89018af445e18ee4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 143 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@              Coverage Diff              @@
##             master    #5918       +/-   ##
=============================================
+ Coverage     54.75%   66.80%   +12.05%     
- Complexity     3453     4505     +1052     
=============================================
  Files           538      611       +73     
  Lines         15583    16967     +1384     
  Branches       2961     3277      +316     
=============================================
+ Hits           8532    11335     +2803     
+ Misses         6694     5183     -1511     
- Partials        357      449       +92     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5918?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/maths/FindKthNumber.java b/src/main/java/com/thealgorithms/maths/FindKthNumber.java
index a9b26767..138d4b95 100644
--- a/src/main/java/com/thealgorithms/maths/FindKthNumber.java
+++ b/src/main/java/com/thealgorithms/maths/FindKthNumber.java
@@ -1,5 +1,7 @@
 package com.thealgorithms.maths;
 
+import java.util.Collections;
+import java.util.PriorityQueue;
 import java.util.Random;
 
 /**
@@ -62,4 +64,19 @@ public final class FindKthNumber {
         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();
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/FindKthNumberTest.java b/src/test/java/com/thealgorithms/maths/FindKthNumberTest.java
index 21285a52..ca69e66c 100644
--- a/src/test/java/com/thealgorithms/maths/FindKthNumberTest.java
+++ b/src/test/java/com/thealgorithms/maths/FindKthNumberTest.java
@@ -14,21 +14,30 @@ public class FindKthNumberTest {
         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
@@ -36,6 +45,8 @@ public class FindKthNumberTest {
         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
@@ -43,8 +54,10 @@ public class FindKthNumberTest {
         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) {