#include<stdio.h>
int balanced_partition(int *arr, int len)
{
int sm = 0, i, j;
for(i = 0;i < len; i++)
sm += arr;
if(sm % 2 != 0)
return 0;
int ans[sm/2 + 1][len + 1];
for(i = 0; i <= len; i++)
ans[0] = 1;
for(i = 1; i <= sm/2; i++)
ans[0] = 0;
for(i = 1; i <= sm/2; i++)
{
for(j = 1;j <= len; j++)
{
ans[j] = ans[j-1];
if(i >= arr[j - 1])
ans[j] = ans[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) O(sum)
b) O(n)
c) O(sum * n)
d) O(sum + n)
What is the time complexity of the following dynamic programming implementation of the balanced partition problem where
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
What is the time complexity of the following dynamic programming implementation of the balanced partition problem where
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!