Skip to content
Official PYQ · 2017

GATE CS 2017 Set 2 Q03 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

Match the algorithms with their time complexities:

AlgorithmsTime ComplexityP. Tower of Hanoi with n disksi. Θ(n2)Q. Binary Search given n sorted numbersii. Θ(nlog⁡n)R. Heap sort given n numbers at the worst caseiii. Θ(2n)S. Addition of two n×n matricesiv. Θ(log⁡n)\begin{array}{|l|l|}\hline \textbf{Algorithms} & \textbf{Time Complexity} \\ \hline \text{P. Tower of Hanoi with $n$ disks} & \text{i. $\Theta (n^2)$} \\ \hline \text{Q. Binary Search given $n$ sorted numbers} & \text{ii. $\Theta (n \log n)$} \\ \hline \text{R. Heap sort given $n$ numbers at the worst case} & \text{iii. $\Theta (2^n)$} \\ \hline \text{S. Addition of two $n\times n$ matrices} & \text{iv. $\Theta (\log n)$} \\ \hline \end{array}

Choose one option
View worked solution

Answer: C

We can determine the time complexity of each algorithm as follows:

  • P. Tower of Hanoi with nn disks: The recurrence relation for the number of moves is given by T(n)=2T(n−1)+1T(n) = 2T(n-1) + 1 with T(1)=1T(1) = 1. Solving this recurrence yields T(n)=2n−1T(n) = 2^n - 1, which is in Θ(2n)\Theta(2^n).
    Thus, P → (iii).
  • Q. Binary Search given nn sorted numbers: In each step, the search space is halved. The recurrence relation is T(n)=T(n/2)+Θ(1)T(n) = T(n/2) + \Theta(1), which solves to Θ(log⁡n)\Theta(\log n).
    Thus, Q → (iv).
  • R. Heap sort given nn numbers at the worst case: Heap sort consists of building a max-heap (which takes O(n)O(n) time) followed by nn extractions of the maximum element (each taking O(log⁡n)O(\log n) time). The overall worst-case time complexity is Θ(nlog⁡n)\Theta(n \log n).
    Thus, R → (ii).
  • S. Addition of two n×nn \times n matrices: To add two n×nn \times n matrices, we perform element-wise addition for each of the n2n^2 positions. This requires n2n^2 additions, resulting in a time complexity of Θ(n2)\Theta(n^2).
    Thus, S → (i).

Combining these matches, we get: P → (iii), Q → (iv), R → (ii), S → (i), which corresponds to Option C.

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

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