Kth Maximum and Minimum in an Array

Nilesh Saini
1 min readMay 22, 2022

--

Different methods to find kth max and min in an array

Photo by Karina Vorozheeva on Unsplash

Max and Min Binary Heap

  1. Build a max and min-heap. It will take O(n) time.
  2. Extract max and min k times.
  3. Time complexity O(n + k*logN).

Sorting the Array:

  1. If the given array is unsorted then sort the array in ascending order using sort() method.
  2. Kth max can be obtained on the index: array.length - k
  3. Kth min can be obtained on the index: k - 1
  4. Time Complexity O(N logN), Space Complexity O(1)

Using Hashmap

  1. We will use the javascript object as a map.
  2. Store each item of the array in the map.
  3. Kth max can be obtained on the index: arr.length - k
  4. Kth min can be obtained on the index: k - 1
  5. Time Complexity O(n), Space Complexity O(n)

--

--

Nilesh Saini
Nilesh Saini

Written by Nilesh Saini

Web Developer/ Front-end engineer who loves solving Rubik's Cube