Kth Maximum and Minimum in an Array
1 min readMay 22, 2022
Different methods to find kth max and min in an array
Max and Min Binary Heap
Sorting the Array:
- If the given array is unsorted then sort the array in ascending order using sort() method.
- Kth max can be obtained on the index: array.length - k
- Kth min can be obtained on the index: k - 1
- Time Complexity O(N logN), Space Complexity O(1)
Using Hashmap
- We will use the javascript object as a map.
- Store each item of the array in the map.
- Kth max can be obtained on the index: arr.length - k
- Kth min can be obtained on the index: k - 1
- Time Complexity O(n), Space Complexity O(n)
Also read,
How to build max and min binary heap?