Please solve these doubts related to quiz on Dynamic Programming

  1. predict the complexity of the code:
    vector<vector > v(100,vector (100,-1) );
    int func(int n,int m)
    {
    if((n==0)||(m==0))
    {
    return 0;
    }
    else
    {
    if(dp[m][n]!=-1)
    {
    return dp[m][n];
    }
    else
    {
    return func(n-1,m-2)+func(n-2,m-1);
    }

    }

}
I think that because they are not putting any element in dp so dp is not in use, which implies order should be 2^n??
2) Efficient (Memory) Table size of Matrix Chain Multipilcation of Matrix A1 to AN:
O(n^2)
B. O(n^2/2)
C. O(n)
None of these

  1. Given two sequences A and B, what will be the length of the longest subsequence present in both of them if we define L[m][n] , LCS of strings A[1….m] and B [1……n] ?
    L[i][j]=L[i-1][j-1]+1 , when A[i]==B[j]
    L[i][j]=(abs(L[i-1][j]-L[i][j-1])+L[i][j-1]+L[i-1][j]) /2 , when A[i]!=B[j]
    L[i][j]=max(L[i-1][j],L[i]][j-1]) , when A[i]!=B[j]
    All of these
    I do not understand what is meant by L[i][j]=(abs(L[i-1][j]-L[i][j-1])+L[i][j-1]+L[i-1][j]) /2 , when A[i]!=B[j]
  2. Given an array A consisting of n integers , we define beauty of the array as maximum sum of some contiguous elements with performing atmost one operation which is , you can multiply any subarray with ‘x’ but only once.
    What will be the maximum subarray sum for this input
    N=5,x=-2
    A=[-3,8,-2,1,-6]
    (Bonus :: Can you write the effiicient code for it ?)
    0
    22
    24
    12
    I am not able to understand the question. Please explain.
  3. Given two strings , what could be the best complexity that you can solve in , the K-ordered LCS of the given strings ?
    A k-ordered LCS is defined to be the LCS of two sequences if you are allowed to change at most k elements in the first sequence to any value you wish to.
    Defining N and M as the length of the given strings respectively.
    O(NMlogk)
    O(NMk)
    O(N*M)
    O(NklogM)

hello @sahilkhan2312000131

yeah we are never storing result in dp array ,so the dp table is useless ,time complexity will remain exponential.

big notation wise both option A , B are correct,
becuase O(N^2) == O(C * N ^2 ),

N^2/2 is more accurate becuase we can do space optimisation by declaring a variable size matrix (matrix whose row are of different size).
image

upd : thats actually calculating L[i][j]=max(L[i-1][j],L[i]][j-1]) this only.
break that into 3 cases,
case -> L[i-1][j] ==L[i][j-1]
abs term will be 0
in that case L[i][j]=L[i][j-1] or L[i-1][j] , since both are equal we can do (L[i-1][j]+ L[i][j-1])/2 -> L[i][j-1] or L[i-1][j] which is same as max of max(L[i-1][j],L[i]][j-1])

case 2 : L[i][j-1] > L[i-1][j] but note L[i][j-1] will always be 1+ L[i-1][j]
now apply the above formula u will again get max(L[i-1][j],L[i]][j-1])
case 3 : L[i][j-1] < L[i-1][j] but note L[i-1][j] will always be 1+ L[i][j-1]
now apply the above formula u will again get max(L[i-1][j],L[i]][j-1])

u r given an array , we can pick any subarray and can multiply all its element with x (optional step)
after this operation u need to tell what is the maximum sum subarray u can achieve

spend some time on this problem (it is also given in ur assignment)
hint - > its a small variation of lcs

1)Given an array A consisting of n integers , we define beauty of the array as maximum sum of some contiguous elements with performing atmost one operation which is , you can multiply any subarray with ‘x’ but only once.
What will be the maximum subarray sum for this input
N=5,x=-2
A=[-3,8,-2,1,-6]
(Bonus :: Can you write the effiicient code for it ?)
0
22
24
12
I am not able to get how is the answer coming out to be 22, I am getting the answer as 14.

[-3,8,-2,1,-6] multiply highlighted part with x

[-3, 8 , 4, -2, 12 ]

now check this subarraY 8 , 4, -2, 12 it has sum 22

Coding Blocks IDE
Can you see is the code correct for this question?

no it is not correct, ur solution is taking subset instead of subarray for multiplication with x.
refer this -> link

Can you give me any test case for which my code is working wrong?

3
-2 10 -1
-2

output should be 14

Not able to understand the code

first finish all ur assignment problems and then attempt this

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.