Suppose you are given two maxheaps one of size m and one of
Suppose you are given two max-heaps, one of size m and one of size n. Design an efficient algorithm to merge them.
Solution
Algorithm to merge given two max heaps:
First of all create a new array and put the elements of both heaps in arbitrary order.
For example we have two sorted arrays First one is of size m+n but containing only m elements
i.e.,
This is our array of size m+n and here only m elements are given and NA means not assigned and the values are present in second array.
Array containing n elements.
If we merge these two arrays we will get output as
| 3 | NA | 7 | NA | NA | 12 | NA |
