• recategorized by
6,118 views
11 11 votes

What is the complexity of finding $50^{th}$ smallest element in an already constructed binary min-heap?

  1. $\Theta(1)$
  2. $\Theta (\log n)$
  3. $\Theta (n)$
  4. $\Theta (n \log n)$

5 Answers

Best answer
11 11 votes

It is constant.as long as number is independent of n it can be found with k×k-1/2 Comparisons.

The 50 th smallest element must be within first 50 levels of minheap. 

for first min :0 comp(root itself)  

For second min: 0(first min)  +1 comp(at level 2) =1

For 3 rd min : 0(first min) +1(2 nd min) +2(to compare the child elements of 2nd min and the other node left at 2 nd level : 2 Comparisons for comparing 3 elements to find min )=3

Similarly

For 50th smalest=0+1+2...+49=

49*50/2=1225 Comparisons which is constant  

We need not delete the elements in which case the complexity will be 50 logn

• edited by
1 1 vote
Deletion of min element from min-heap can be done in  logn time. To find the 50th min element we have to perform 49 deletion of min element from the binary min-heap.

Therefore, total time to extract 50th min element  = 49 lgn

                                   = theta(lgn)
1 1 vote
If traversal in the min heap allowed then it is constant time or else it is log(n)
1 1 vote

Finding minimum element in a min heap takes O(1) time. To find the 50th smallest element in a min heap needs to delete first 49 smaller elements and then finding the next smaller element, i.e. 50th smallest element. 

Deleting smallest element (root element in a min heap) takes O(logn) time. Total time to delete 49 smaller elements is O(49*logn) = O(logn). Next finding the next min element takes O(1) time. 

Next inserting all the 49 deleted elements need to be inserted back into the heap. That takes O(49*logn). The total time complexity thus becomes O(logn).

Position:
Show:

Related questions

1 1 vote
1 1 answer
1.6k
1.6k views
8 8 votes
3 3 answers
5.9k
5.9k views
Kapil asked Sep 4, 2016
5,938 views
In a min-heap with n elements1). The 7th smallest element can be found in time, if duplicates are allowed ?2). The 7th distinct smallest element can be found in time, I...
0 0 votes
0 0 answers
2.0k
2.0k views
Shubhanshu asked Oct 18, 2017
2,048 views
In a binary min heap with n elements, the 7th smallest element can be found in _____ ?Answer given is O(logn) and solution:-Delete the 1st smallest element O(logn)Delete ...
1 1 vote
1 answers 1 answer
3.4k
3.4k views
gmrishikumar asked Dec 1, 2018
3,380 views
What is the time complexity to find the Kth largest element in a Min-Heap? Or equivalently, What is the time complexity to find Kth smallest element in Max-Heap?