Skip to content
Official PYQ · 2008

GATE CS 2008 Q47 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

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

Choose one option
View worked solution

Answer: B

To insert nn additional elements into an existing binary heap of size nn, we have two main approaches:

  1. One-by-one Insertion: If we insert the nn elements one after another, each insertion takes O(extlogk)O( ext{log } k) time, where kk is the heap size. For nn insertions, the total time complexity would be: ∑k=n2n−1Θ(log⁡k)=Θ(nlog⁡n)\sum_{k=n}^{2n-1} \Theta(\log k) = \Theta(n \log n)
  2. Batch Insertion (Build-Heap): Since we do not need to maintain the heap property after each individual insertion (as indicated by "not necessarily one after another"), we can:
    • Append the nn new elements to the end of the existing heap array in O(n)O(n) time.
    • Run the standard bottom-up Build-Heap algorithm on the combined array of size 2n2n.
    The time complexity of building a heap of size NN from scratch is Θ(N)\Theta(N). Here, N=2nN = 2n, so the total time required is: Θ(2n)=Θ(n)\Theta(2n) = \Theta(n)

Since we seek the most efficient method to achieve the final heap, the optimal total time required is Θ(n)\Theta(n).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions