Here is my approach :
int main()
{
int a=1,k=1,n=3;
while(k>1)
{
k--;
a=(a<<1) | 1;
}
while(n>1)
{
a=a<<1;
n--;
}
printf("%d",a);
return 0;
}
On Sat, Jul 9, 2011 at 11:14 PM, 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.
>
>
--
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.