Advertisement
shabbyheart

all possible print that sub set is not equal sum

Nov 16th, 2019
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int w[100000],x[100000],m,n;
  4. class SumofsubSet{
  5. public:
  6.     void sum(int s,int k,int r)
  7.     {
  8.         x[k] = 1;
  9.         if( s + w[k] != m)
  10.         {
  11.             display(k);
  12.         }
  13.         if(s+w[k] == m)
  14.         {
  15.             //display(k);
  16.         }
  17.         else if (s+w[k]<=m && k<n)
  18.             sum(s+w[k],k+1,r-w[k]);
  19.         //cout<<k<<" "<<endl;
  20.         if((s+w[k+1])!=m && k<n )
  21.         {
  22.             x[k] = 0;
  23.             sum(s,k+1,r-w[k]);
  24.         }
  25.     }
  26.     void display(int k)
  27.     {
  28.         for(int i=1;i<=k;i++)
  29.         {
  30.             if( x[i] == 1)
  31.                 cout<<w[i]<<" ";
  32.         }
  33.         cout<<endl;
  34.     }
  35. };
  36. int main()
  37. {
  38.     //freopen("input.txt","r",stdin);
  39.     SumofsubSet ss;
  40.     cin>>n>>m;
  41.     int total = 0;
  42.     for(int i =1; i<=n;i++)
  43.     {
  44.         cin>>w[i];
  45.         total = total+ w[i];
  46.     }
  47.     ss.sum(0,1,total);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement