Hi,
this is the code i came up with..
#include<stdio.h>
#include<conio.h>
int doMyWork(int k,int n);
int main()
{
int k,n,nth_number;
printf("\nEnter k and n\n");
scanf("%d%d",&k,&n);
nth_number=doMyWork(k,n);
printf("\nThe nth number is %d\n",nth_number);
getch();
return(1);
}
int doMyWork(int k,int n)
{
int startnum=0,c,v;
while(n)
{
for (c=0,v=startnum ; v; c++)
{
v &= v - 1;
}
if(c==k)
{
n--;
}
startnum++;
}
return(startnum-1);
}
On Sun, Jul 10, 2011 at 11:44 AM, anurag <[email protected]> wrote:
> You are given two integers n and k
> k signifies number of set bits i.e. if k = 3 then output should have 3
> set bits.
> Output should be the nth smallest number having k set bits
> for example
> k=1 and n=3
> output should be
> 4 (00000100)
>
> --
> 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.
>
>
--
Warm Regards,
Sunny T
--
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.