Smallest Number with k bits set will be the number with least significant k
bits set
ie. K=3 000111
K=4 0001111
and to find nth
we can use this<http://groups.google.com/group/algogeeks/msg/2b64c4f96fa3598e>
TC: O(n)
On Sun, Jul 10, 2011 at 2:13 PM, Sunny T <[email protected]> wrote:
> i agree.. it is not the the fastest algorithm for this problem.. it is a
> basic solution..
>
>
> On Sun, Jul 10, 2011 at 2:09 PM, Anurag Aggarwal <
> [email protected]> wrote:
>
>> Sunny
>> Don.t you think your method is very slow as you are checking every number
>> that for number of set bits and if it is a equal to desired than you are
>> decreasing n i.e. required number.
>> Even if when n=1 and k=32 your solution will start with 0 and go up to
>> 2^31 but the answer could be found in single iteration.
>>
>> So is there any way to do it a little faster.
>>
>> On Sun, Jul 10, 2011 at 1:08 PM, Sunny T <[email protected]> wrote:
>>
>>> sorry ankit... ur solution works only for.. k=1 and n=3...
>>> try for k=2 and n=6.. then the output should be 12...
>>> similarly for k=3 n=1.. the output should be...7...
>>>
>>> so plz correct ur code..
>>>
>>>
>>> On Sun, Jul 10, 2011 at 12:38 PM, ankit sambyal
>>> <[email protected]>wrote:
>>>
>>>> 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.
>>>>
>>>>
>>>
>>>
>>> --
>>> 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.
>>>
>>
>> --
>> 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.
>
--
Sunny Aggrawal
B-Tech IV year,CSI
Indian Institute Of Technology,Roorkee
--
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.