Question

Match the algorithms with their time complexities:

Algorithm Time complexity
(P) Towers of Hanoi with nn disks (i) Θ(n2)\Theta(n^2)
(Q) Binary search given nn sorted numbers (ii) Θ(nlog⁡n)\Theta(n \log n)
(R) Heap sort given nn numbers at the worst case (iii) Θ(2n)\Theta(2^n)
(S) Addition of two n×nn \times n matrices (iv) Θ(log⁡n)\Theta(\log n)
Choose one option
Official answer and solution
Official answer
C
From
Official GATE 2017 answer key

Solution

Identify the standard worst-case time complexities of the four algorithms.

Analyze each algorithm:

  • (P) Towers of Hanoi with nn disks: The recurrence is T(n)=2T(n−1)+1T(n) = 2T(n-1) + 1, which gives T(n)=2n−1=Θ(2n)T(n) = 2^n - 1 = \Theta(2^n). This matches (iii).
  • (Q) Binary search on nn sorted numbers: Halves the search space at each comparison, giving recurrence T(n)=T(n/2)+O(1)T(n) = T(n/2) + O(1), so T(n)=Θ(log⁡n)T(n) = \Theta(\log n). This matches (iv).
  • (R) Heap sort on nn numbers (worst case): Building the heap takes O(n)O(n), and each of the nn extractions takes O(log⁡n)O(\log n) heapify time, yielding a worst-case time complexity of Θ(nlog⁡n)\Theta(n \log n). This matches (ii).
  • (S) Addition of two n×nn \times n matrices: Requires adding n×n=n2n \times n = n^2 pairs of corresponding elements, taking Θ(n2)\Theta(n^2) time. This matches (i).

Combining the matches gives P →\to (iii), Q →\to (iv), R →\to (ii), S →\to (i), matching option C.

Final answer

C

Common trap

Confusing matrix addition (which is O(n2)O(n^2)) with standard matrix multiplication (which is O(n3)O(n^3)).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 2
Question
3 of 65, printed as CS Q.3
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.