-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsetSum.cpp
63 lines (47 loc) · 1.06 KB
/
subsetSum.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
#include<vector>
using namespace std;
int subSequenceSum(int i,int rSum,int &noOfSolutions,vector<int> res,vector<int> &S,int n);
int main(){
int n;
cout<<"Enter the number of elements in Set : \n";
cin>>n;
vector<int> S (n,0);
cout<<"Enter the elements : \n";
for(int &x : S)
{
cin>>x;
}
int sum = 0;
cout<<"Enter the subsequence Sum : \n";
cin>>sum;
int noOfSolutions = 0;
noOfSolutions = subSequenceSum(0,sum,noOfSolutions,{},S,n);
if(!noOfSolutions){
cout<<"There does not exist any subset with sum "<<sum<<"\n";
}
else{
cout<<"Total No of subsets with sum "<<sum<<" = "<<noOfSolutions<<"\n";
}
}
int subSequenceSum(int i,int rSum,int &noOfSolutions,vector<int> res,vector<int> &S,int n)
{
if(i>=n)
{
if(rSum == 0)
{
cout<<"Subset with sum : \n";
for(int &x: res)
{
cout<<x<<" ";
}
cout<<"\n";
noOfSolutions++;
}
return noOfSolutions;
}
subSequenceSum(i+1,rSum,noOfSolutions,res,S,n);
res.push_back(S[i]);
subSequenceSum(i+1,rSum-S[i],noOfSolutions,res,S,n);
return noOfSolutions;
}