Interview Bootcamp
Vote

0% completed

​

Solution: Kth Smallest Number

Problem Statement

Given an unsorted array of numbers, find Kth smallest number in it.

Please note that it is the Kth smallest number in the sorted order, not the Kth distinct element.

Note: For a detailed discussion about different approaches to solve this problem, take a look at Kth Smallest Number.

Example 1:

Input: [1, 5, 12, 2, 11, 5], K = 3
Output: 5
Explanation: The 3rd smallest number is '5', as the first two smaller numbers are [1, 2].

Example 2:

.....

.....

.....

Like the course? Get enrolled and start learning!
B

Baraa Attabbaa

· 3 years ago

Initializing the min-heap with all numbers will take O(N)

Show 1 reply
U

Utkarsh Gupta

· 3 years ago

Time Complexity of alternative approach would be O(N log(N)). Insertion takes O(logN) time. In the alternate approach we are first inserting all the numbers into the heap which would result in O(Nlog(n)).

F

First

· 4 years ago

// if heap has more than 'k' numbers, remove one number if (this.minHeap.length > this.k) { this.minHeap.pop(); } How does this loop enough times to pop the minHeap if this is an if statement?

Show 2 replies

Reading Progress

0%


Vote for new content