Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int w[100000],x[100000],m,n;
- class SumofsubSet{
- public:
- void sum(int s,int k,int r)
- {
- x[k] = 1;
- if(s+w[k] == m)
- {
- display(k);
- // for(int i=1;i<=k;i++)
- // {
- // cout<<x[i]<<" ";
- // }
- // cout<<endl;
- }
- else if (s+w[k]+w[k+1]<=m)
- sum(s+w[k],k+1,r-w[k]);
- if((((s+r)-w[k])>=m) &&(s+w[k+1])<=m)
- {
- x[k] = 0;
- sum(s,k+1,r-w[k]);
- }
- }
- void display(int k)
- {
- for(int i=1;i<=k;i++)
- {
- if( x[i] == 1)
- cout<<w[i]<<" ";
- }
- cout<<endl;
- }
- };
- int main()
- {
- freopen("input.txt","r",stdin);
- SumofsubSet ss;
- cin>>n>>m;
- int total = 0;
- for(int i =1; i<=n;i++)
- {
- cin>>w[i];
- total = total+ w[i];
- }
- ss.sum(0,1,total);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement