Question: Consider the following problem: Given a sequence of integers, find the longest subsequence whose elements are in increasing order. More concretely, the input is an

Consider the following problem: Given a sequence of integers, find the longest subsequence whose elements are in increasing order. More concretely, the input is an integer array \( A[1\ldots n]\), and we need to compute the longest possible sequence of indices \(1\leq i_{1}n) return 0 ;
else if (A[i]>=A[j]) return f(i,j+1) ;
else {
int skip = f(i,j+1) ;
int take =1+f(j,j+1) ;
return max(skip,take) ;
}
```
Which can be implemented dynamically using the following algorithm:
```
function g(A[1..n]){
A[0]=-\infty
for i =0 to n
L[i][n+1]=0
for j = n downto 1
for i =0 to j-1
keep =1+ L[j][j+1]
skip = L[i][j+1]
if A[i]\geq A[j]
L[i][j]= skip
else
L[i][j]= max(keep,skip)
```
a) Modify the dynamic algorithm so that you can remember if you want to "keep" or "skip" each number.
b) Code the modified dynamic algorithm in C or Java.
c) Write function printSolution(int A[], int n) which calls your implementation above and then print the numbers in the longest increasing sub-sequence. The sub-sequence must be printed in the order it appeared in the original sequence (from lowest index to highest.)
Consider the following problem: Given a sequence

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!