const int setSize = 20;
int set[setSize] =
{ 5,9,1,3,4,2,6,7,11,10,13,15,19,22,25,31,33,37,39,40};
const int sum = 150;
int rec[setSize];
int recCount = 0;
int subset=0;
void search(int *set, int setSize, int sum)
{
int i;
if (sum == 0)
{
printf("Subset %d: {%d", ++subset,rec[0]);
for(i = 1; i < recCount; ++i)
printf(",%d", rec[i]);
printf("}\n");
}
else if (sum > 0)
{
for(i = 0; i < setSize; ++i)
{
rec[recCount++] = set[i];
search(set, i, sum-set[i]);
--recCount;
}
}
}
int main(int argc, char* argv[])
{
search(set, setSize, sum);
return 0;
}
On Apr 18, 6:16 am, kamlesh yadav <[email protected]> wrote:
> given an array of elements (all elements are unique ) , given a sum
> s find all the subsets having sum s.
>
> for ex array {5,9,1,3,4,2,6,7,11,10}
>
> sum is 10
>
> possible subsets are {10}, {6,4} ,{7,3} {5,3,2}
> {6,3,1} etc. there can be many more.
> also find the total number of these subsets
--
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.