Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@maksadbek
Forked from jitsceait/subsetsum_dyn.c
Last active April 8, 2018 02:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maksadbek/14f9475beb709f7e0bba to your computer and use it in GitHub Desktop.
Save maksadbek/14f9475beb709f7e0bba to your computer and use it in GitHub Desktop.
#include<stdlb.h>
#include<stdio.h>
int isSubsetSum1(int arr[], int n, int sum)
{
/* The value of subset[i][j] will be true if there is a
subset of set[0..j-1] */
int subset[sum+1][n+1];
int i,j;
/* If sum ==0, there will be empty set satisfying that condition
hence row with sum = 0 will have all true values. */
for (i = 0; i <= n; i++)
subset[0][i] = true;
/* If sum is not 0 and set is empty, no subset is there */
for (i = 1; i <= sum; i++)
subset[i][0] = false;
for (i = 1; i <= sum; i++)
{
for ( j = 1; j <= n; j++)
{
/* If there is subset with j-1 elements, copy value */
subset[i][j] = subset[i][j-1];
/* Now if the latest element can be added */
if (i >= arr[j-1])
subset[i][j] = subset[i][j] ||subset[i-arr[j-1]][j-1];
}
}
return subset[sum][n];
}
/* Driver program */
int main(){
int set[] = {1,3,5,4,6};
int n = sizeof(set)/sizeof(set[0]);
int subset[n];
printf("%d", isSubsetSum(set, n, 10));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment