Q: Binary subset rank with maximum k consecutive 0 Consider N = 6 bits
We have a set S = 2^6 = 64 binary combinations.
We want to consider a subset T of the set S based on the following criteria -

*

*There must be <= k consecutive 0s in the subset T.

Accordingly,

*

*If the binary number is present in subset T, calculate its rank (in binary format)

*If the binary number is not present in subset T, return 0 (in binary format)

Note: We want to calculate the rank only by iterating the N bits of the given binary number, and NOT by iterating 2^N numbers.
Here is the pseudo-code -
string GetBinaryRank(string strBinaryNumber, int max_k_0s)
{
    string strRankBinary = "";
    
    if(strBinaryNumber.Contains(<= max_k_0s))
    {
       //calculate strRankBinary by iterating over the N bits of strBinaryNumber
       for(int i = 0; i < strBinaryNumber.Length; i++)
       {
           if(max_k_0s == 1)
           {
               //calculate rank
               //binary rank will be 5 bits
               strRankBinary = "xxxxx";
           }
           else if(max_k_0s == 2)
           {
               //calculate rank
               //binary rank will be 6 bits
               strRankBinary = "yyyyyy";
           }
       }
    }
    else
    {
        strRankBinary = "000000";
    }

    return strRankBinary;
}

We want to do the rank and unrank operations for the binary number, and determine the maximum number of bits required for "strRankBinary"
Also, we want to generalize the code for "N"-bit numbers and maximum "k" consecutive 0s.
Depending on the maximum rank value, the strRankBinary must contain only the required number of maximum bits.
In the examples given below,

*

*If max_k_0s = 1, maximum rank will be 21, which is 10101 (5 bits) in
binary.

*If max_k_0s = 2, maximum rank will be 44, which is 101100 (6 bits) in
binary.

Example 1 (max 1 consecutive 0s in 6-digit binary value):
Number      Rank    Binary rank
-------------------------------
000000       0      00000
000001       0      00000
000010       0      00000
000011       0      00000
000100       0      00000
000101       0      00000
000110       0      00000
000111       0      00000
001000       0      00000
001001       0      00000
001010       0      00000
001011       0      00000
001100       0      00000
001101       0      00000
001110       0      00000
001111       0      00000
010000       0      00000
010001       0      00000
010010       0      00000
010011       0      00000
010100       0      00000
010101       1      00001
010110       2      00010
010111       3      00011
011000       0      00000
011001       0      00000
011010       4      00100
011011       5      00101
011100       0      00000
011101       6      00110
011110       7      00111
011111       8      01000
100000       0      00000
100001       0      00000
100010       0      00000
100011       0      00000
100100       0      00000
100101       0      00000
100110       0      00000
100111       0      00000
101000       0      00000
101001       0      00000
101010       9      01001
101011      10      01010
101100       0      00000
101101      11      01011
101110      12      01100
101111      13      01101
110000       0      00000
110001       0      00000
110010       0      00000
110011       0      00000
110100       0      00000
110101      14      01110
110110      15      01111
110111      16      10000
111000       0      00000
111001       0      00000
111010      17      10001
111011      18      10010
111100       0      00000
111101      19      10011
111110      20      10100
111111      21      10101

Example 2 (max 2 consecutive 0s in 6-digit binary value):
Number      Rank    Binary rank
-------------------------------
000000       0      000000
000001       0      000000
000010       0      000000
000011       0      000000
000100       0      000000
000101       0      000000
000110       0      000000
000111       0      000000
001000       0      000000
001001       1      000001
001010       2      000010
001011       3      000011
001100       4      000100
001101       5      000101
001110       6      000110
001111       7      000111
010000       0      000000
010001       0      000000
010010       8      001000
010011       9      001001
010100      10      001010
010101      11      001011
010110      12      001100
010111      13      001101
011000       0      000000
011001      14      001110
011010      15      001111
011011      16      010000
011100      17      010001
011101      18      010010
011110      19      010011
011111      20      010100
100000       0      000000
100001       0      000000
100010       0      000000
100011       0      000000
100100      21      010101
100101      22      010110
100110      23      010111
100111      24      011000
101000       0      000000
101001      25      011001
101010      26      011010
101011      27      011011
101100      28      011100
101101      29      011101
101110      30      011110
101111      31      011111
110000       0      000000
110001       0      000000
110010      32      100000
110011      33      100001
110100      34      100010
110101      35      100011
110110      36      100100
110111      37      100101
111000       0      000000
111001      38      100110
111010      39      100111
111011      40      101000
111100      41      101001
111101      42      101010
111110      43      101011
111111      44      101100

