GATE CS 2026 · Set-2 · Asymptotic Analysis & Recurrences

MCQ · 2 marks · Problem PROG-DIVC-0066

Consider a table \(T\), where the elements \(T[i][j], 0 \leq i, j \leq n\), represent the cost of the optimal solutions of different subproblems of a problem that is being solved using a dynamic programming algorithm. The recursive formulation to compute the table entries is as follows:\[\begin{array}{ll} T[0][k]=T[k][0]=1 & \text { for } k=0,1,2, \ldots, n \\ T[i][j]=2 T[i-1][j]+3 T[i][j-1] & \text { for } 1 \leq i, j \leq n \end{array}\]Consider the following two algorithms to compute entries of \(T\). Assume that for both the algorithms, for all \(0 \leq i, j \leq n, T[i][j]\) has been initialized to \(1\).\[\begin{aligned} &\text{Algorithm } B_1: \\ &\quad \text{For } i = 1,2,\ldots,n \\ &\quad\quad \text{For } j = 1,2,\ldots,n \\ &\quad\quad\quad T[i][j] = 2T[i-1][j] + 3T[i][j-1] \\ &\text{Algorithm } B_2: \\ &\quad \text{For } s = 2,3,\ldots,2n \\ &\quad\quad \text{For } i = 1,2,\ldots,n \\ &\quad\quad\quad \text{For } j = 1,2,\ldots,n \\ &\quad\quad\quad\quad \text{If } i + j = = s \\ &\quad\quad\quad\quad\quad T[i][j] = 2T[i-1][j] + 3T[i][j-1] \end{aligned}\]Algorithm \(B_{k}, k \in\{1,2\}\) is said to be correct if and only if it calculates the correct values of \(T[i][j]\), for all \(0 \leq i, j \leq n\), (as per the recursive formulation) at the end of the execution of the algorithm \(B_{k}\).Which one of the following statements is true?
  1. A
    Both algorithms \(B_{1}\) and \(B_{2}\) are correct
  2. B
    Algorithm \(B_{1}\) is correct, but algorithm \(B_{2}\) is incorrect
  3. C
    Algorithm \(B_{2}\) is correct, but algorithm \(B_{1}\) is incorrect
  4. D
    Both algorithms \(B_{1}\) and \(B_{2}\) are incorrect

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solution\[\boxed{\text{An order is correct if it computes } T[i-1][j] \text{ and } T[i][j-1] \text{ before } T[i][j]}\]\(B_1\) fills row by row, left to right: the entry above is in an earlier row and the entry to the left is earlier in the same row, so both are final. Correct.\(B_2\) fills the anti-diagonals \(i+j=s\) in increasing order of \(s\): both needed entries lie on the anti-diagonal \(s-1\) (or on the boundary), which is already complete. Correct.Answer: A

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com