adplus-dvertising
frame-decoration

Question

Consider the following dynamic programming implementation.
Which of the following lines should be added to complete the code?
#include<stdio.h>
#include<string.h>
int max(int a, int b)
{
      if(a > b)
        return a;
      return b;
}
int min_ins(char *s)
{
      int len = strlen(s), i, j;
      int arr[len + 1][len + 1];
      char rev[len + 1];
      strcpy(rev, s);
      strrev(rev);
      for(i = 0;i <= len; i++)
         arr[i][0] = 0;
      for(i = 0; i <= len; i++)
         arr[0][i] = 0;
      for(i = 1; i <= len; i++)
      {
          for(j = 1; j <= len; j++)
          {
              if(s[i - 1] == rev[j - 1])
                 arr[i][j] = arr[i - 1][j - 1] + 1;
              else
                 arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]);
          }
      }
      return _____________;
}
int main()
{
      char s[] = "abcda";
      int ans = min_ins(s);
      printf("%d",ans);
      return 0;
}

a.

arr[len][len].

b.

len + arr[len][len].

c.

len

d.

len – arr[len][len].

Answer: (d).len – arr[len][len].

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. Which of the following lines should be added to complete the code?

Similar Questions

Discover Related MCQs

Q. Given a 2D matrix, find a submatrix that has the maximum sum. Which of the following methods can be used to solve this problem?

Q. In which of the following cases, the maximum sum rectangle is the 2D matrix itself?

Q. In which of the following cases, the maximum sum rectangle can be a 1 x 1 matrix containing the largest element?

Q. Consider a matrix in which all the elements are non-zero(at least one positive and at least one negative element). In this case, the sum of the elements of the maximum sum rectangle cannot be zero.

Q. Consider the 2×3 matrix {{1,2,3},{1,2,3}}. What is the sum of elements of the maximum sum rectangle?

Q. Consider the 2×2 matrix {{-1,-2},{-3,-4}}. What is the sum of elements of the maximum sum rectangle?

Q. Consider the 3×3 matrix {{2,1,-3},{6,3,4},{-2,3,0}}. What is the sum of the elements of the maximum sum rectangle?

Q. What is the time complexity of the brute force implementation of the maximum sum rectangle problem?

Q. The dynamic programming implementation of the maximum sum rectangle problem uses which of the following algorithm?

Q. Given an array, check if the array can be divided into two subsets such that the sum of elements of the two subsets is equal. This is the balanced partition problem. Which of the following methods can be used to solve the balanced partition problem?

Q. In which of the following cases, it is not possible to have two subsets with equal sum?

Q. What is the time complexity of the brute force algorithm used to solve the balanced partition problem?

Q. Consider a variation of the balanced partition problem in which we find two subsets such that |S1 – S2| is minimum. Consider the array {1, 2, 3, 4, 5}. Which of the following pairs of subsets is an optimal solution for the above problem?

Q. What is the sum of each of the balanced partitions for the array {5, 6, 7, 10, 3, 1}?

Q. You are given n dice each having f faces. You have to find the number of ways in which a sum of S can be achieved. This is the dice throw problem. Which of the following methods can be used to solve the dice throw problem?

Q. You have n dice each having f faces. What is the number of permutations that can be obtained when you roll the n dice together?

Q. You have 3 dice each having 6 faces. What is the number of permutations that can be obtained when you roll the 3 dice together?

Q. You have 2 dice each of them having 6 faces numbered from 1 to 6. What is the number of ways in which a sum of 11 can be achieved?

Q. There are n dice with f faces. The faces are numbered from 1 to f. What is the minimum possible sum that can be obtained when the n dice are rolled together?

Q. There are n dice with f faces. The faces are numbered from 1 to f. What is the maximum possible sum that can be obtained when the n dice are rolled together?