34,698 views
70 70 votes

We have a binary heap on $n$ elements and wish to insert $n$ more elements (not necessarily one after another) into this heap. The total time required for this is

  1. $\Theta(\log n)$

  2. $\Theta(n)$

  3. $\Theta(n\log n)$

  4. $\Theta(n^2)$

9 Answers

Best answer
102 102 votes

An insert operation on a binary heap takes $\mathcal{O}(\log n)$ time, but an alternative approach we can use. which requires us to insert $n$ elements in heap without any computation i.e. in constant time. after which we can apply Heapify operation(this operation creates heap in linear time) on the array of those element and Hence obtain a Heap in $\mathcal{O}(n)$ time.


Here "not necessarily one after another" should mean that we can insert $n$ elements at once and not necesaairly have to wait for first insert to be completed before doing second.

Correct Answer: $B$

• edited by
15 15 votes
Answer is B.

By build Heap method.
15 15 votes
to insert 1 element into heap it will takes O(logn) time,

so to insert n elements into heap it will take O(nlogn) time.

but there is also a better way means we can apply build heap method for 2n elements it will take O(2n) =O(n).

so b is ans
4 4 votes

The worst case time complexity for insertion in a binary heap is O(Logn) (Refer Wiki). So inserting n elements in a heap of size n should take Θ(nlogn) time.
But choice (B) seems to be more appropriate answer. One of the solution of O(n) complexity can be to take the ‘n’ elements of the heap and other ‘n’ elements together and construct heap in O(2n) = O(n)

1 1 vote

Answer is (B). 

We can imagine that these first 'n' elements are also not a heap. And then simply add these next 'n' elements at the end of the array. And consder total 2n elements in an array and then call Build Heap(). We know that build heap takes O(n) times. So for these 2n elements it will also takes O(n) times. So is the answer.

Answer:
Position:
Show:

Related questions

74 74 votes
4 answers 4 answers
36.3k
36.3k views
Kathleen asked Sep 12, 2014
36,272 views
Which of the following are NOT true in a pipelined processor?Bypassing can handle all RAW hazardsRegister renaming can eliminate all register carried WAR hazardsControl h...
43 43 votes
6 answers 6 answers
22.2k
22.2k views
go_editor asked Apr 23, 2016
22,185 views
Consider the following C functions:int f1 (int n) { if(n == 0 || n == 1) return n; else return (2 * f1(n-1) + 3 * f1(n-2)); } int f2(int n) { int i; int X[N], Y[N], Z[N];...
54 54 votes
7 answers 7 answers
30.9k
30.9k views
Kathleen asked Sep 12, 2014
30,917 views
Consider the following C functions:int f1 (int n) { if(n == 0 || n == 1) return n; else return (2 * f1(n-1) + 3 * f1(n-2)); } int f2(int n) { int i; int X[N], Y[N], Z[N];...
102 102 votes
9 answers 9 answers
56.0k
56.0k views
Kathleen asked Sep 12, 2014
56,001 views
The minimum number of comparisons required to determine if an integer appears more than $\frac{n}{2}$ times in a sorted array of $n$ integers is$\Theta(n)$$\Theta(\log n)...