adplus-dvertising
frame-decoration

Question

Consider the following dynamic programming implementation of the Knapsack problem.
Which of the following lines completes the below code?
#include<stdio.h>
int find_max(int a, int b)
{
      if(a > b)
         return a;
      return b;
}
int knapsack(int W, int *wt, int *val,int n)
{
     int ans[n + 1][W + 1];
     int itm,w;
     for(itm = 0; itm <= n; itm++)
         ans[itm][0] = 0;
     for(w = 0;w <= W; w++)
        ans[0][w] = 0;
     for(itm = 1; itm <= n; itm++)
     {
          for(w = 1; w <= W; w++)
          {
               if(wt[itm - 1] <= w)
                  ans[itm][w] = ______________;
               else
                  ans[itm][w] = ans[itm - 1][w];
          }
     }
     return ans[n][W];
}
int main()
{
     int w[] = {10,20,30}, v[] = {60, 100, 120}, W = 50;
     int ans = knapsack(W, w, v, 3);
     printf("%d",ans);
     return 0;
}

a.

find_max(ans[itm – 1][w – wt[itm – 1]] + val[itm – 1], ans[itm – 1][w])

b.

find_max(ans[itm – 1][w – wt[itm – 1]], ans[itm – 1][w])

c.

ans[itm][w] = ans[itm – 1][w];

d.

none of the mentioned

Answer: (a).find_max(ans[itm – 1][w – wt[itm – 1]] + val[itm – 1], ans[itm – 1][w])

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. Consider the following dynamic programming implementation of the Knapsack problem. Which of the following lines completes the below code?

Similar Questions

Discover Related MCQs

Q. Which of the following methods can be used to solve the matrix chain multiplication problem?

Q. Which of the following is the recurrence relation for the matrix chain multiplication problem where mat[i-1] * mat[i] gives the dimension of the ith matrix?

Q. Consider the two matrices P and Q which are 10 x 20 and 20 x 30 matrices respectively. What is the number of multiplications required to multiply the two matrices?

Q. Consider the matrices P, Q and R which are 10 x 20, 20 x 30 and 30 x 40 matrices respectively. What is the minimum number of multiplications required to multiply the three matrices?

Q. Consider the matrices P, Q, R and S which are 20 x 15, 15 x 30, 30 x 5 and 5 x 40 matrices respectively. What is the minimum number of multiplications required to multiply the four matrices?

Q. Consider the brute force implementation in which we find all the possible ways of multiplying the given set of n matrices. What is the time complexity of this implementation?

Q. Which of the following methods can be used to solve the longest common subsequence problem?

Q. Consider the strings “PQRSTPQRS” and “PRATPBRQRPS”. What is the length of the longest common subsequence?

Q. Which of the following problems can be solved using the longest subsequence problem?

Q. Longest common subsequence is an example of ____________

Q. What is the time complexity of the brute force algorithm used to find the longest common subsequence?

Q. Which of the following is the longest common subsequence between the strings “hbcfgmnapq” and “cbhgrsfnmq” ?

Q. Which of the following methods can be used to solve the longest palindromic subsequence problem?

Q. Which of the following strings is a palindromic subsequence of the string “ababcdabba”?

Q. For which of the following, the length of the string is equal to the length of the longest palindromic subsequence?

Q. What is the length of the longest palindromic subsequence for the string “ababcdabba”?

Q. What is the time complexity of the brute force algorithm used to find the length of the longest palindromic subsequence?

Q. For every non-empty string, the length of the longest palindromic subsequence is at least one.

Q. Longest palindromic subsequence is an example of ______________

Q. Which of the following methods can be used to solve the edit distance problem?