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.
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 –
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 firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count total set bits in all numbers from 1 to N | Set 3
- Iterative approach to print all combinations of an Array
- Count binary strings with k times appearing adjacent two set bits
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Number of Binary Strings of length N with K adjacent Set Bits
- Calculate the number of set bits for every number from 0 to N
- Count of pairs in an Array with same number of set bits
- Product of all sorted subsets of size K using elements whose index divide K completely
- Print all longest common sub-sequences in lexicographical order
- Print all distinct characters of a string in order (3 Methods)
- Find maximum topics to prepare in order to pass the exam
- Maximum sum by picking elements from two arrays in order | Set 2
- Count even length binary sequences with same sum of first and second half bits
- Find all distinct subset (or subsequence) sums of an array | Set-2
- Minimum number of deletions to make a sorted sequence
- Count columns to be deleted to make each row sorted
- Alternate XOR operations on sorted array
- Minimum increment or decrement operations required to make the array sorted
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Longest increasing subsequence which forms a subarray in the sorted representation of the array
Improved By : ps_gotnochill