Thursday, October 20, 2011

Given an integer N, there are 2^N binary codewords of length N. All of them are taken and sorted to create a sequence: Sort by the number of 1-bits in the codeword bit-string. If there is a tie, break tie so that the smaller number comes first in the output sequence

Example  and Testcases :You would be given 2 integers N (<=10) and K (1 <= K <= 2^N)
Output the codeword with index K into this sequence
The input is from stdin and output to stdout

Sample Testcases:
Input #00:
3 5
Output #00:
011
Explanation:
For N = 3, the sequence consists of {000,001,010,100,011,101,110,

111}. The 5th indexed number would be 011

Input #01:
7 127
Output #01:
1111110

No comments :