Skip to content

Commit 82a562a

Browse files
Add sum of subset problem using DP (TheAlgorithms#2451)
1 parent 4e1e4a1 commit 82a562a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Sum_Of_Subset {
2+
public static void main(String[] args){
3+
4+
int[] arr = { 7, 3, 2, 5, 8 };
5+
int Key = 14;
6+
7+
if (subsetSum(arr, arr.length - 1, Key)) {
8+
System.out.print("Yes, that sum exists");
9+
}
10+
else {
11+
System.out.print("Nope, that number does not exist");
12+
}
13+
}
14+
public static boolean subsetSum(int[] arr, int num, int Key)
15+
{
16+
if (Key == 0) {
17+
return true;
18+
}
19+
if (num < 0 || Key < 0) {
20+
return false;
21+
}
22+
23+
boolean include = subsetSum(arr, num - 1, Key - arr[num]);
24+
boolean exclude = subsetSum(arr, num - 1, Key);
25+
26+
return include || exclude;
27+
}
28+
}

0 commit comments

Comments
 (0)