Dynamic Programming
![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)
Answer & solution
Official answer: A
Compute the displayed Li from right to left, reusing the already-computed Li+1. This is a dynamic program with linear time and the maximum Li as output, so A. Its recurrence computes strictly increasing contiguous runs; it is not the general longest increasing subsequence algorithm when skipped elements are permitted. A suitable evaluation order is needed even though the source writes the index range without an order.
For the recurrence to run, process i=n-2,n-3,...,0. If adjacent entries increase, the run beginning at i is the current element followed by the already-solved run at i+1; otherwise it ends immediately. Each table entry takes constant work, and the final maximum takes linear work. For A=[1,3,2,4], every increasing contiguous run has length 2, although the noncontiguous subsequence [1,2,4] has length 3. This illustrates the important limitation of the displayed recurrence.