It looks like finding all subsets of a set which satisfy a condition
(sum is ZERO).
Using bit approach
i.e.
01101 == >> 120,150, 316
10100 ==>> -466, 150
int main(int argc, char *argv[])
{
int sum = 0;
int tmp=0;
int i=0;
int a[10]={-466, 120, 150, 421, 316};
int length = 5;
int no_of_subset=31;
char subset[50];
while (no_of_subset>=1)
{
sum=0;
tmp=no_of_subset;
subset[0]=0;
for(i=length-1;i>=0;i--)
{
if(tmp & 1)
{
sum += a[i];
sprintf(subset,"%s %d,",subset,a[i]);
}
tmp = tmp >> 1;
}
if(sum==0)
printf("subset: %s ",subset);
no_of_subset--;
}
return 0;
}
We can also use recursive approach (finding subsets recursively)
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.