-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSumOfSubset.java
More file actions
33 lines (26 loc) · 766 Bytes
/
Copy pathSumOfSubset.java
File metadata and controls
33 lines (26 loc) · 766 Bytes
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
public class SumOfSubset {
public static boolean solve (int set[], int value, int sum, int depth) {
if (depth == set.length) {
if (sum == value)
return true;
else
return false;
}
sum+=set[depth];
if(solve(set, value, sum, depth+1))
return true;
sum-=set[depth];
if(solve(set, value, sum, depth+1))
return true;
return false;
}
public static void main(String[] args) {
// Sample Subset
int set[] = {1, 3, 4, 5, 8, 10};
int value = 12;
if (solve(set, value, 0, 0))
System.out.println("Sebset Exist!");
else
System.out.println("Not Exist!");
}
}