#include<stdio.h>
#include<math.h>
int set_bits(int a)
{
int c=0;
while(a)
{
if(a & 1)
c++;
a >>=1;
}
return c;
}
void sub(int a[],int n,int k)
{
int i; int x,c=0,j;
x = pow(2,n);
for(i=0;i<x;i++) //this statement is exec 2^n times
{
if(set_bits(i) == k) // This is satisfied nCk times
{
c=0;
printf("{ ");
for(j=0;c<k;j++) /* this loop runs n times
(no.of bits in 2^n - 1) */
if(i>>j & 1)
{
printf("%d ",a[n-j-1]);
c++;
}
printf("}\n");
}
}
}
int main()
{
int a[]= {1,2,3,4};
int k=2,n=4;
sub(a,n,k);
getch();
}
Time Complexity is quite high. O(n(2^n))
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/algogeeks/-/jWHXwaHrTGEJ.
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.