Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5f317df

Browse files
Subset Sum Problem
1 parent 9d3754c commit 5f317df

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Amit Bansal - @amitbansal7 */
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
//Subset Sum Problem
5+
6+
int main()
7+
{
8+
int set[] = {2, 3, 7, 8, 10};
9+
int sz = sizeof(set) / sizeof(set[0]);
10+
int s = 11;
11+
bool DP[s + 1][sz];
12+
13+
//If sum is 0 answer is always true.
14+
for (int i = 0; i < sz; i++)
15+
DP[0][i] = 1;
16+
17+
for (int i = 0; i < sz; i++)
18+
{
19+
for (int n = 1; n <= s; n++)
20+
{
21+
if (n < set[i])
22+
DP[n][i] = i < 1 ? 0 : DP[n][i - 1];
23+
24+
else if (n == set[i])
25+
DP[n][i] = 1;
26+
27+
else
28+
DP[n][i] = i < 1 ? 0 : DP[n][i - 1] || DP[n - set[i]][i - 1];
29+
}
30+
}
31+
32+
DP[11][sz - 1] ? cout << "For 11 : Yes" << endl : cout << "For 11 : No" << endl;
33+
DP[6][sz - 1] ? cout << "For 6 : Yes" << endl : cout << "For 6 : No" << endl;
34+
35+
return 0;
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /