in DS edited by
1,421 views
3 votes
3 votes

Consider M1 and M2 be two complete binary tree which satisfy max-heap property, each of size ‘n’. What is the time complexity to combine both M1 and M2 such that combine tree will be min heap tree?

  • O (n log n)
  • O (n)
  • O (n2)
  • O (n2 log n)
in DS edited by
1.4k views

2 Comments

M1 and M2 are contain total of 2n elements.(stores it in the array and construct min heap)

to construct min heap with n elements is o(nlogn)

=o(2nlog2n)=o(nlogn)
0
0

I also think answer should be 0(nlogn).  @

0
0

2 Answers

4 votes
4 votes
We have 2n number of elements and building a min heap using 2n elemets take O(2n) i.e.O(n) time

3 Comments

ypu call build heap which is O(n)
and then min heapify which is O(logn)
therefore O(nlogn) right?
0
0
@A_i_$_h

it will be O(n) only

in build heap procedure only, it will be converted to heap, then why call min heapify..
0
0
got it :)
0
0
0 votes
0 votes
Since it is given that the tree satisfy max heap.So just the reverse array element and copy it in size of 2n array.Then it would be a min heap.

1 comment

your approach is wrong, in max heap not necessary that last element should be minimum.
0
0

Related questions