Similar questions 1 more →
Two valid dynamic-programming orders
Dynamic Programming
![Question 39, GATE 2026: T[0][k]=T[k][0]=1 for 0≤k≤n; for 1≤i,j≤n use T[i][j]=2T[i−1][j]+3T[i][j−1]. All cells initially equal 1. B1 loops i=1…n then j=1…n and updates each interior cell. B2 loops s=2…2n, then all i,j=1…n, updating only when i+j=s. Which is correct? A both; B only B1; C only B2; D neither.](../../../assets/questions/CS-2026-S2-Q39-1.webp)
Answer & solution
Official answer: A
Givens and goal
T[0][k]=T[k][0]=1 for 0≤k≤n; for 1≤i,j≤n use T[i][j]=2T[i−1][j]+3T[i][j−1]. All cells initially equal 1. B1 loops i=1…n then j=1…n and updates each interior cell. B2 loops s=2…2n, then all i,j=1…n, updating only when i+j=s. Which is correct? A both; B only B1; C only B2; D neither.
Approach
Check whether the dependencies are ready before each update.
Step-by-step solution
1. Each interior cell depends only on its upper neighbor (i−1,j) and left neighbor (i,j−1). The coefficients 2 and 3 do not affect the ordering requirement.
2. In B1, row i−1 has already been completed when row i begins. Within row i, j increases, so the left neighbor has already been computed. Boundary neighbors retain their initialized correct values.
3. In B2, both dependencies have coordinate sum i+j−1=s−1. They were processed in the previous diagonal layer, unless they lie on a preinitialized boundary. Every interior cell has a unique sum between 2 and 2n and is updated exactly once.
4. For a small check, T[1][1]=2+3=5; T[1][2]=2×1+3×5=17; T[2][1]=2×5+3×1=13; T[2][2]=2×17+3×13=73. Either order produces these values.
5. Both are correct, A. B1 performs Θ(n²) work, whereas the stated B2 scans n² pairs for Θ(n) values of s and takes Θ(n³). Correctness does not require equal efficiency.
![Question 35, GATE 2024: The distance of an integer array is the least number of entries replaced by arbitrary integers to make it nondecreasing. Find the distance of [2,5,3,1,4,2,6].](../../../assets/questions/CS-2024-S2-Q35-1.webp)

![Question 25, GATE 2019: For A=[−5,−10,6,3,−1,−2,13,4,−9,−1,4,12,−3,0], find the maximum S(i,j)=sum of A[k] for k=i through j, with 0≤i≤j<14. The chosen elements must form a contiguous segment.](../../../assets/questions/CS-2019-Q35-1.webp)




![Question 25, GATE 2011: An array algorithm initializes L[n-1]=1 and for 0≤i≤n-2 defines L[i]=1+L[i+1] when A[i]<A[i+1], otherwise L[i]=1. It returns max(L[0],...,L[n-1]). The source calls this the longest monotonically increasing sequence. Which statement is TRUE?
(A) It uses dynamic programming
(B) It has linear complexity and uses branch and bound
(C) It has nonlinear polynomial complexity and uses branch and bound
(D) It uses divide and conquer](../../../assets/questions/CS-2011-Q35-1.webp)




![Question 54, GATE 2009: Store the LCS recurrence in L[m+1, n+1]. Which is true? A every cell must first be zero; B increasing row-major or column-major computation works; C neither works; D L[p, q] must precede L[r, s] whenever p<r OR q<s.](../../../assets/questions/CS-2009-Q54-1.webp)
![Question 54, GATE 2009: Store the LCS recurrence in L[m+1, n+1]. Which is true? A every cell must first be zero; B increasing row-major or column-major computation works; C neither works; D L[p, q] must precede L[r, s] whenever p<r OR q<s.](../../../assets/questions/CS-2009-Q54-2.webp)
![Question 54, GATE 2009: Store the LCS recurrence in L[m+1, n+1]. Which is true? A every cell must first be zero; B increasing row-major or column-major computation works; C neither works; D L[p, q] must precede L[r, s] whenever p<r OR q<s.](../../../assets/questions/CS-2009-Q54-3.webp)
![Question 80, GATE 2008: Given positive integers S={a₁,…,aₙ} and target W>0, a subset-sum dynamic program uses Boolean X with n rows and W+1 columns. X[i,j] is true iff some subset of {a₁,…,aᵢ} sums to j, for 1≤i≤n and 0≤j≤W. Which recurrence is valid for 2≤i≤n and aᵢ≤j≤W?
(A) X[i,j]=X[i−1,j]∨X[i,j−aᵢ]
(B) X[i,j]=X[i−1,j]∨X[i−1,j−aᵢ]
(C) X[i,j]=X[i−1,j]∧X[i,j−aᵢ]
(D) X[i,j]=X[i−1,j]∧X[i−1,j−aᵢ]](../../../assets/questions/CS-2008-Q80-1.webp)
![Question 80, GATE 2008: Given positive integers S={a₁,…,aₙ} and target W>0, a subset-sum dynamic program uses Boolean X with n rows and W+1 columns. X[i,j] is true iff some subset of {a₁,…,aᵢ} sums to j, for 1≤i≤n and 0≤j≤W. Which recurrence is valid for 2≤i≤n and aᵢ≤j≤W?
(A) X[i,j]=X[i−1,j]∨X[i,j−aᵢ]
(B) X[i,j]=X[i−1,j]∨X[i−1,j−aᵢ]
(C) X[i,j]=X[i−1,j]∧X[i,j−aᵢ]
(D) X[i,j]=X[i−1,j]∧X[i−1,j−aᵢ]](../../../assets/questions/CS-2008-Q80-2.webp)
![Question 81, GATE 2008: Given positive integers S={a₁,…,aₙ} and target W>0, a subset-sum dynamic program uses Boolean X with n rows and W+1 columns. X[i,j] is true iff some subset of {a₁,…,aᵢ} sums to j, for 1≤i≤n and 0≤j≤W. Which entry, if TRUE, implies there is a subset summing to W? (This preserves the printed one-way wording.)
(A) X[1,W]
(B) X[n,0]
(C) X[n,W]
(D) X[n−1,n]](../../../assets/questions/CS-2008-Q81-1.webp)
![Question 81, GATE 2008: Given positive integers S={a₁,…,aₙ} and target W>0, a subset-sum dynamic program uses Boolean X with n rows and W+1 columns. X[i,j] is true iff some subset of {a₁,…,aᵢ} sums to j, for 1≤i≤n and 0≤j≤W. Which entry, if TRUE, implies there is a subset summing to W? (This preserves the printed one-way wording.)
(A) X[1,W]
(B) X[n,0]
(C) X[n,W]
(D) X[n−1,n]](../../../assets/questions/CS-2008-Q81-2.webp)
![Question 81, GATE 2008: Given positive integers S={a₁,…,aₙ} and target W>0, a subset-sum dynamic program uses Boolean X with n rows and W+1 columns. X[i,j] is true iff some subset of {a₁,…,aᵢ} sums to j, for 1≤i≤n and 0≤j≤W. Which entry, if TRUE, implies there is a subset summing to W? (This preserves the printed one-way wording.)
(A) X[1,W]
(B) X[n,0]
(C) X[n,W]
(D) X[n−1,n]](../../../assets/questions/CS-2008-Q81-3.webp)


![Question 16(a), GATE 2000: Array f[0..M] starts at zero. fib(n) checks n>M for error, returns 1 for n=0 or 1, then executes if([1]) return [2]; t=fib(n−1)+fib(n−2); [3]; return t. Assume 0<=n<=M and exact arithmetic. Fill the three boxes.](../../../assets/questions/CS-2000-Q80-1.webp)
![Question 16(a), GATE 2000: Array f[0..M] starts at zero. fib(n) checks n>M for error, returns 1 for n=0 or 1, then executes if([1]) return [2]; t=fib(n−1)+fib(n−2); [3]; return t. Assume 0<=n<=M and exact arithmetic. Fill the three boxes.](../../../assets/questions/CS-2000-Q80-2.webp)
![Question 16(b), GATE 2000: Array f[0..M] starts at zero. fib(n) checks n>M for error, returns 1 for n=0 or 1, then executes if([1]) return [2]; t=fib(n−1)+fib(n−2); [3]; return t. Assume 0<=n<=M and exact arithmetic. With memoization filled correctly, find the running time of the first fib(n) computation.](../../../assets/questions/CS-2000-Q81-1.webp)
![Question 16(b), GATE 2000: Array f[0..M] starts at zero. fib(n) checks n>M for error, returns 1 for n=0 or 1, then executes if([1]) return [2]; t=fib(n−1)+fib(n−2); [3]; return t. Assume 0<=n<=M and exact arithmetic. With memoization filled correctly, find the running time of the first fib(n) computation.](../../../assets/questions/CS-2000-Q81-2.webp)
![Question 16(b), GATE 2000: Array f[0..M] starts at zero. fib(n) checks n>M for error, returns 1 for n=0 or 1, then executes if([1]) return [2]; t=fib(n−1)+fib(n−2); [3]; return t. Assume 0<=n<=M and exact arithmetic. With memoization filled correctly, find the running time of the first fib(n) computation.](../../../assets/questions/CS-2000-Q81-3.webp)
