fork download
  1. #include <iostream>
  2. #include<cmath>
  3. #include<map>
  4. using namespace std;
  5.  
  6. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  7.  
  8. map<int,bool> visit;
  9. map<int,int>subset;
  10. int idx=0;
  11.  
  12. int main(int argc, char** argv) {
  13. int n;
  14. //cout<<"Enter the number of elements in the array";
  15. cin>>n;
  16. int a[n+1],i;
  17. //cout<<"Enter the array elements : ";
  18. for(i=0;i<n;i++)
  19. {
  20. cin>>a[i];
  21. visit[a[i]]=1;
  22. }
  23.  
  24. //cout<<"All the subsets are : \n";
  25. int k=0;
  26. //cout<<k<<" : empty "<<endl;;
  27.  
  28. k=1;
  29. int max = pow(2,n) - 1;
  30. int next,rightbit,nexthigh,y;
  31.  
  32. for(int i=1;i<=n;i++)
  33. {
  34. k = pow(2,i)-1;
  35. // cout<<i<<" bits set : ";
  36. while(k<=max)
  37. {
  38. subset[idx++]=k;
  39. // cout<<k<<" ";
  40. rightbit = (k&(-k));
  41. nexthigh= rightbit+k;
  42.  
  43. y= nexthigh + (((nexthigh^k)/rightbit)>>2);
  44.  
  45. k=y;
  46. }
  47. // cout<<endl;
  48. }
  49.  
  50. for(i=1;i<=idx;i++)
  51. {
  52. // cout<<"The bit pattern corresponds to : ";
  53. for(int j=0;j<n;j++)
  54. {
  55. if(i&(1<<j))
  56. {
  57. cout<<a[j]<<" ";
  58. }
  59. }
  60.  
  61. cout<<endl;
  62. }
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 3436KB
stdin
4
3 1 4 2
stdout
3 
1 
3 1 
4 
3 4 
1 4 
3 1 4 
2 
3 2 
1 2 
3 1 2 
4 2 
3 4 2 
1 4 2 
3 1 4 2