8000 Merge pull request #1103 from HardikSoni11/patch-4 · glitches-coder/Algorithms@769a812 · GitHub
[go: up one dir, main page]

Skip to content

Commit 769a812

Browse files
Merge pull request VAR-solutions#1103 from HardikSoni11/patch-4
Create SubSetSum.java
2 parents a6cc2c0 + 760421f commit 769a812

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class SubSetSum {
2+
static boolean isSubsetSum(int set[],
3+
int n, int sum)
4+
{
5+
// Base Cases
6+
if (sum == 0)
7+
return true;
8+
if (n == 0)
9+
return false;
10+
if (set[n - 1] > sum)
11+
return isSubsetSum(set, n - 1, sum);
12+
return isSubsetSum(set, n - 1, sum)
13+
|| isSubsetSum(set, n - 1, sum - set[n - 1]);
14+
}
15+
16+
/* Driver program to test above function */
17+
public static void main(String args[])
18+
{
19+
int set[] = { 3, 34, 4, 12, 5, 2 };
20+
int sum = 9;
21+
int n = set.length;
22+
if (isSubsetSum(set, n, sum) == true)
23+
System.out.println("Subset found"
24+
+ " with given sum");
25+
else
26+
System.out.println("No subset with"
27+
+ " given sum");
28+
}
29+
}

0 commit comments

Comments
 (0)
0