Monday, February 27, 2012

Write an algorithm that prints an unordered sets of k elements chosen from a set of size n.

Example, let the size of the give set be n and set = {0, 1, 2, 3, 4} and we need to find all the subsets of size 3, then there will be a total of 10 such subsets given as:

{0, 1, 2}
{0, 1, 3}
{0, 1, 4}
{0, 2, 3}
{0, 2, 4}
{0, 3, 4}
{1, 2, 3}
{1, 2, 4}
{1, 3, 4}
{2, 3, 4}


2 comments :

Atul anand said...

void combination_with_limit(int arr[],int limit,int len,int j,int i)
{
static int result[100];
int k;

if(j==limit)
{
printf("\n");
for(k=0;k<limit;k++)
{
printf("%d ",result[k]);

}
return;

}
while(i<len)
{
result[j]=arr[i];
combination_with_limit(arr,limit,len,j+1,i+1);
i++;
}
}

call :-
combination_with_limit(arr,limit,arrlen,0,0);

Unknown said...

@Atul , Yeah , Thats correct one :)