-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path22-subset-sum-dynamic.c
49 lines (40 loc) · 941 Bytes
/
22-subset-sum-dynamic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int subset_sum(int a[], int n, int sum)
{
int ss[n+1][sum+1];
int i,j;
// When sum is 0, empty set is the solution
for(i = 0; i <= n; i++)
ss[i][0] = 1;
// When there are no array elements, subsets are not possible
for(j = 1; j <= sum; j++)
ss[0][j] = 0;
for(i = 1; i <= n; i++) {
for(j = 1; j <= sum; j++){
if(ss[i-1][j] == 1)
ss[i][j] = 1;
else
{
if(a[i-1] > j)
ss[i][j] = 0;
else
ss[i][j] = ss[i - 1][j - a[i-1]];
}
}
}
return ss[n][sum];
}
int main()
{
int n = 5;
int a[5] = {1, 2, 3, 5, 7};
int sum = 9;
int result=subset_sum(a,n,sum);
if(result)
printf("Subset found\n");
else
printf("Subset not found\n");
return 0;
}