adplus-dvertising
frame-decoration

Question

What is the value stored in ans[3][3] when the following code is executed?
#include<stdio.h>
int balanced_partition(int *arr, int len)
{
     int sm = 0, i, j;
     for(i = 0;i < len; i++)
      sm += arr[i];
     if(sm % 2 != 0)
        return 0;
     int ans[sm/2 + 1][len + 1];
     for(i = 0; i <= len; i++)
      ans[0][i] = 1;
     for(i = 1; i <= sm/2; i++)
      ans[i][0] = 0;
     for(i = 1; i <= sm/2; i++)
     {
         for(j = 1;j <= len; j++)
         {
             ans[i][j] = ans[i][j-1];
             if(i >= arr[j - 1])
                ans[i][j] = ans[i][j] || ans[i - arr[j - 1]][j - 1];
         }
     }
     return ans[sm/2][len];
}
int main()
{
     int arr[] = {3, 4, 5, 6, 7, 1}, len = 6;
     int ans = balanced_partition(arr,len);
     if(ans == 0)
        printf("false");
     else
        printf("true");
     return 0;
}

a.

0

b.

1

c.

-1

d.

-2

Answer: (b).1

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the value stored in ans[3][3] when the following code is executed?

Similar Questions

Discover Related MCQs

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?

Q. There are 10 dice having 5 faces. The faces are numbered from 1 to 5. What is the number of ways in which a sum of 4 can be achieved?

Q. What is time complexity of the above dynamic programming implementation of the dice throw problem where f is the number of faces, n is the number of dice and s is the sum to be found?

Q. What is space complexity of the above dynamic programming implementation of the dice throw problem where f is the number of faces, n is the number of dice and s is the sum to be found?

Q. You are given a boolean expression which consists of operators &, | and ∧ (AND, OR and XOR) and symbols T or F (true or false). You have to find the number of ways in which the symbols can be parenthesized so that the expression evaluates to true. This is the boolean parenthesization problem. Which of the following methods can be used to solve the problem?

Q. Consider the expression T & F | T. What is the number of ways in which the expression can be parenthesized so that the output is T (true)?

Q. Consider the expression T & F ∧ T. What is the number of ways in which the expression can be parenthesized so that the output is T (true)?

Q. Consider the expression T | F ∧ T. In how many ways can the expression be parenthesized so that the output is F (false)?

Q. Which of the following gives the total number of ways of parenthesizing an expression with n + 1 terms?

Q. What is the maximum number of ways in which a boolean expression with n + 1 terms can be parenthesized, such that the output is true?

Q. How many different insertion sequences of the key values using the same hash function and linear probing will result in the hash table shown above?

Q. If ' h ' is a hashing function and it is used to hash ' n ' keys into a table of size ' m ' where n <= m . What is the expected number of collisions involving a particular key ' x ' ?

Q. Hashing technique which allocates fixed number of buckets is classified as

Q. Consider a hash table of size seven, with starting index zero, and a hash function (3x + 4)mod7. Assuming the hash table is initially empty, which of the following is the contents of the table when the sequence 1, 3, 8, 10 is inserted into the table using closed hashing? Note that _ denotes an empty location in the table.