I came along this programming interview question and I am unsure if my answer is right. I was unable to find a proper answer to this question. Here is the question,
Let H1 and H2 be two (binary) max-heaps with n1 and n2 elements respectively. If every element in H1 is larger than every element in H2, design an algorithm that merges these two heaps into one (binary) max-heap in O(n2) time (assume that both H1 and H2 are large enough to hold n1 + n2 elements)
So, I was thinking that since every element in H1 is greater than every element in H2 then we can store the merged max heap in H1. So, all we have to do is simply get the first element from H2, at position 0 in the array for H2, and then insert that element into H1, append to the end of the array of H1 making it a leaf in H1. We can continuously do that for every element in H2. I suppose that once we start adding elements from H2 as children to elements of H2 then we will have to start checking whether that child is less than the parent and if not we swap them. I'm assuming that since adding an element, without calling heapify, and swapping when necessary will give us a complexity of O(n2).
So, is my solution accurate? If not any help will be much appreciated. Please tell me if any part of my solution is unclear so I can clarify.