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

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 = 3Output:001 010 100 011 101 110 111Input:K = 4Output:0001 0010 0100 1000 0011 0101 0110 1001 1010 1100 0111 1011 1101 1110 1111Input:K = 5Output: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 table ` `vector<string> DP[K][K]; ` ` ` `// Function to find all combinations k-bit numbers with ` `// n-bits set where 1 <= n <= k ` `void` `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 code ` `int` `main() ` `{ ` ` ` `int` `k = 5; ` ` ` `findBitCombinations(k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

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.

## Recommended Posts:

- Program for Fibonacci numbers
- Program to find amount of water in a given glass
- Find if a string is interleaved of two other strings | DP-33
- Find all distinct palindromic sub-strings of a given string
- Find the minimum cost to reach destination using a train
- Count numbers from 1 to n that have 4 as a digit
- Compute sum of digits in all numbers from 1 to n
- Find minimum number of coins that make a given value
- Find length of the longest consecutive path from a given starting character
- Find number of solutions of a linear equation of n variables
- Total number of non-decreasing numbers with n digits
- Count of n digit numbers whose sum of digits equals to given sum
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count even length binary sequences with same sum of first and second half bits
- Find the longest path in a matrix with given constraints