829 views
0 votes
0 votes

In a min heap time complexity for

1: Find the 7th smallest element

2: Delete the 7th smallest element

1 Answer

Best answer
6 votes
6 votes

 

1. in min heap, we know that 7th minimum will be present maximum till 7th level, and there will 127 elements till 7th level, so we have to do only constant time comparison, and it will take O(1) time.


NOTE: if elements are repeated then also finding 7th smallest will be O(1) .

consider this, 1,1,1,1,1,2,2,3,3,3,4,4,4,4,4,,5,5,5,5,5,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9 

 its True because

11,12,13,14,15,26,27,3,3,3,4,4,4,4,4,,5,5,5,5,5,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9 

Though duplicates are there but we define kth min element as the element that comes at kth index after sorting the elements of heap. That also will be present at most at the 7th level only.  

Here 7th smallest element is 2 only. 

$\Rightarrow$  if question would have asked 7th distinct smallest then , i think it would be O(n)

2. O(1) for finding 7th smallest + O(1) for deleting it + O(log n) for applying min heapify = O(log n)

Note: I recommend to read the above top 15 comments.

selected by

Related questions

0 votes
0 votes
0 answers
2
iarnav asked Jun 24, 2018
271 views
The number of possible min-heaps containing each value from {1,1,1,1,1,1,1} exactly once is _______This is a variance of Gate 2018 question and how will we deal if all va...
1 votes
1 votes
1 answer
3
iarnav asked Jun 21, 2018
474 views
In a binary Heap of 100 elements time taken to find the 99th element?or in a binary heap on "n" elements, time taken to find (n-1)th element? Note ; I'm not asking about ...
7 votes
7 votes
2 answers
4
Warlock lord asked Dec 5, 2017
1,166 views
From an array of size n , we need to find the k bigger elements. What is the data structure we should use to find k bigger element in best asymptotic complexity? 1.A max ...