What will be the output for the given code? #include bool func (int arr[], int n) { int sum = 0; int i, j; for (i = 0; i < n; i++) sum += arr[i]; if (sum%2 != 0) return false; bool partition[sum/2+1][n+1]; for (i = 0; i <= n; i++) partition[0][i] = true; for (i = 1; i <= sum/2; i++) partition[i][0] = false; for (i = 1; i <= sum/2; i++) { for (j = 1; j <= n; j++) { partition[i][j] = partition[i][j-1]; if (i >= arr[j-1]) partition[i][j] = partition[i][j] || partition[i - arr[j-1]][j-1]; } } return partition[sum/2][n]; } int main() { int arr[] = {3, 3, 4, 4, 7}; int n = sizeof(arr)/sizeof(arr[0]); if (func(arr, n) == true) printf("true"); else printf("false"); return 0; } a) true b) false c) 0 d) error
What will be the output for the given code?
#include <stdio.h>
bool func (int arr[], int n)
{
int sum = 0;
int i, j;
for (i = 0; i < n; i++)
sum += arr[i];
if (sum%2 != 0)
return false;
bool partition[sum/2+1][n+1];
for (i = 0; i <= n; i++)
partition[0][i] = true;
for (i = 1; i <= sum/2; i++)
partition[i][0] = false;
for (i = 1; i <= sum/2; i++)
{
for (j = 1; j <= n; j++)
{
partition[i][j] = partition[i][j-1];
if (i >= arr[j-1])
partition[i][j] = partition[i][j] || partition[i - arr[j-1]][j-1];
}
}
return partition[sum/2][n];
}
int main()
{
int arr[] = {3, 3, 4, 4, 7};
int n = sizeof(arr)/sizeof(arr[0]);
if (func(arr, n) == true)
printf("true");
else
printf("false");
return 0;
}
a) true
b) false
c) 0
d) error
Step by step
Solved in 2 steps