diff --git a/Dynamic Programming/Subset-Sum.cpp b/Dynamic Programming/Subset-Sum.cpp new file mode 100644 index 000000000..12974fb09 --- /dev/null +++ b/Dynamic Programming/Subset-Sum.cpp @@ -0,0 +1,38 @@ + +// Subset Sum Problem (DP) +#include + +using namespace std; +// Returns true if there is a subset of set[] with sun equal to given sum +bool isSubsetSum(int set[], int n, int sum){ + bool subset[n+1][sum+1]; + for (int i = 0; i <= n; i++) + subset[i][0] = true; + for (int i = 1; i <= sum; i++) + subset[0][i] = false; + for (int i = 1; i <= n; i++){ + for (int j = 1; j <= sum; j++){ + if(j= set[i-1]) + subset[i][j] = subset[i-1][j] || subset[i - 1][j-set[i-1]]; + } + } + return subset[n][sum]; +} + +int main() +{ + int n,sum; + //input n - size of set and sum + cin>>n>>sum; + int set[n]; + //input elements of set + for(int i=0;i>set[i]; + if (isSubsetSum(set, n, sum) == true) + cout<<"Found a subset with sum : "<