Sunday, June 1, 2014

Generate all combination of elements of an array : Power Set Algorithm

#include
#include
#define MAX 100010
using namespace std;

int arr[MAX];

void powerset(int arr[], vector v, int start, int end)
{
if(start > end)
return;
v.push_back(arr[start]);
vector::iterator i;
for(i = v.begin(); i
{
cout << *i;
}
cout << "\n";

powerset(arr,v,start+1,end);
v.pop_back();
powerset(arr,v,start+1,end);

}

int main()
{
int T,N;
cin >> T;
while(T-- > 0)
{
cin >> N;

for(int i=0;i
cin >> arr[i];
vector v;
powerset(arr,v,0,N-1);
}
return 0;
}

No comments: