# Data Structure Questions and Answers-Balanced Partition

## Click on any option to know the CORRECT ANSWERS

 Question 1
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?
 A Dynamic programming B Recursion C Brute force D All of the mentioned

Question 1 Explanation:
All of the mentioned methods can be used to solve the balanced partition problem.

 Question 2
In which of the following cases, it is not possible to have two subsets with equal sum?
 A When the number of elements is odd B When the number of elements is even C When the sum of elements is odd D When the sum of elements is even

Question 2 Explanation:
When the sum of all the elements is odd, it is not possible to have two subsets with equal sum.

 Question 3
What is the time complexity of the brute force algorithm used to solve the balanced partition problem?
 A O(1) B O(n) C O(n2) D O(2n)

Question 3 Explanation:
In the brute force implementation, all the possible subsets will be formed. This takes exponential time.

 Question 4
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?
 A {5, 4} & {3, 2, 1} B {5} & {4, 3, 2, 1} C {4, 2} & {5, 3, 1} D {5, 3} & {4, 2, 1}

Question 4 Explanation:
For S1 = {5, 3} and S2 = {4, 2, 1}, sum(S1) - sum(S2) = 1, which is the optimal solution.

 Question 5
Consider the following code:

#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] = .....; } } 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; }

Which of the following lines should be inserted to complete the above code?

 A ans[i - arr[j - 1]][j - 1]. B ans[i][j]. C ans[i][j] || ans[i - arr[j - 1]][j - 1]. D ans[i][j] && ans[i - arr[j - 1]][j - 1].