Find all combinations of k-bit numbers with n bits set where 1 <= n <= k in sorted order - GeeksforGeeks

21 Mar, 2017
Given a number k, find all the possible combinations of k-bit numbers with n-bits set where 1 <= n <= k. The solution should print all numbers with one set bit first, followed by numbers with two bits set,.. up to the numbers whose all k-bits are set. If two numbers have the same number of set bits, then smaller number should come first.
Examples:
Input: K = 3
Output:  
001 010 100 
011 101 110 
111 

Input: K = 4
Output:  
0001 0010 0100 1000 
0011 0101 0110 1001 1010 1100 
0111 1011 1101 1110 
1111 

Input: K = 5
Output:  
00001 00010 00100 01000 10000 
00011 00101 00110 01001 01010 01100 10001 10010 10100 11000 
00111 01011 01101 01110 10011 10101 10110 11001 11010 11100 
01111 10111 11011 11101 11110 
11111 

We need to find all the possible combinations of k-bit numbers with n set bits where 1 <= n <= k. If we carefully analyze, we see that problem can further be divided into sub-problems. We can find all combinations of length k with n ones by prefixing 0 to all combinations of length k-1 with n ones and 1 to all combinations of length k-1 with n-1 ones. We can use Dynamic Programming to save solutions of sub-problems.
Below is C++ implementation of above idea –
// C++ program find all the possible combinations of // k-bit numbers with n-bits set where 1 <= n <= k#include <iostream>#include <vector>using namespace std;// maximum allowed value of K#define K 16  // DP lookup tablevector<string> DP[K][K];  // Function to find all combinations k-bit numbers with // n-bits set where 1 <= n <= kvoid findBitCombinations(int k){    string str = "";          // DP[k][0] will store all k-bit numbers      // with 0 bits set (All bits are 0's)    for (int len = 0; len <= k; len++)     {        DP[len][0].push_back(str);        str = str + "0";    }          // fill DP lookup table in bottom-up manner    // DP[k][n] will store all k-bit numbers      // with n-bits set    for (int len = 1; len <= k; len++)    {        for (int n = 1; n <= len; n++)        {            // prefix 0 to all combinations of length len-1             // with n ones            for (string str : DP[len - 1][n])                DP[len][n].push_back("0" + str);              // prefix 1 to all combinations of length len-1             // with n-1 ones            for (string str : DP[len - 1][n - 1])                DP[len][n].push_back("1" + str);        }    }          // print all k-bit binary strings with    // n-bit set    for (int n = 1; n <= k; n++)     {        for (string str : DP[k][n])            cout << str << " ";          cout << endl;    }}  // Driver codeint main(){    int k = 5;    findBitCombinations(k);      return 0;}
Output:
00000 
00001 00010 00100 01000 10000 
00011 00101 00110 01001 01010 01100 10001 10010 10100 11000 
00111 01011 01101 01110 10011 10101 10110 11001 11010 11100 
01111 10111 11011 11101 11110 
11111

This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
ps_gotnochill
Dynamic Programming
Dynamic Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Optimal Substructure Property in Dynamic Programming | DP-2
Optimal Binary Search Tree | DP-24
Min Cost Path | DP-6
Maximum Subarray Sum using Divide and Conquer algorithm
Greedy approach vs Dynamic programming
3 Different ways to print Fibonacci series in Java
Maximum sum such that no two elements are adjacent
Word Break Problem | DP-32
Top 50 Dynamic Programming Coding Problems for Interviews
How to solve a Dynamic Programming Problem ?