Given a number n, our task is to find all 1 to n bit numbers with no consecutive 1s in their binary representation.

Examples:

Input : n = 4 Output : 1 2 4 5 8 9 10 These are numbers with 1 to 4 bits and no consecutive ones in binary representation. Input : n = 3 Output : 1 2 4 5

We add bits one by one and recursively print numbers. For every last bit, we have two choices.

if last digit in sol is 0 then we can insert 0 or 1 and recur. else if last digit is 1 then we can insert 0 only and recur.

We will use recursion-

- We make a solution vector sol and insert first bit 1 in it which will be the first number.
- Now we check whether length of solution vector is less than or equal to n or not.
- If it is so then we calculate the decimal number and store it into a map as it store numbers in sorted order.
- Now we will have two conditions-
- if last digit in sol is 0 the we can insert 0 or 1 and recur.
- else if last digit is 1 then we can insert 0 only and recur.

numberWithNoConsecutiveOnes(n, sol) { if sol.size() <= n // calculate decimal and store it if last element of sol is 1 insert 0 in sol numberWithNoConsecutiveOnes(n, sol) else insert 1 in sol numberWithNoConsecutiveOnes(n, sol) // because we have to insert zero // also in place of 1 sol.pop_back(); insert 0 in sol numberWithNoConsecutiveOnes(n, sol) }

`// CPP program to find all numbers with no ` `// consecutive 1s in binary representation. ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` `map<` `int` `, ` `int` `> h; ` ` ` `void` `numberWithNoConsecutiveOnes(` `int` `n, vector<` `int` `> ` ` ` `sol) ` `{ ` ` ` `// If it is in limit i.e. of n lengths in ` ` ` `// binary ` ` ` `if` `(sol.size() <= n) { ` ` ` `int` `ans = 0; ` ` ` `for` `(` `int` `i = 0; i < sol.size(); i++) ` ` ` `ans += ` `pow` `((` `double` `)2, i) * ` ` ` `sol[sol.size() - 1 - i]; ` ` ` `h[ans] = 1; ` ` ` ` ` `// Last element in binary ` ` ` `int` `last_element = sol[sol.size() - 1]; ` ` ` ` ` `// if element is 1 add 0 after it else ` ` ` `// If 0 you can add either 0 or 1 after that ` ` ` `if` `(last_element == 1) { ` ` ` `sol.push_back(0); ` ` ` `numberWithNoConsecutiveOnes(n, sol); ` ` ` `} ` `else` `{ ` ` ` `sol.push_back(1); ` ` ` `numberWithNoConsecutiveOnes(n, sol); ` ` ` `sol.pop_back(); ` ` ` `sol.push_back(0); ` ` ` `numberWithNoConsecutiveOnes(n, sol); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `vector<` `int` `> sol; ` ` ` ` ` `// Push first number ` ` ` `sol.push_back(1); ` ` ` ` ` `// Generate all other numbers ` ` ` `numberWithNoConsecutiveOnes(n, sol); ` ` ` ` ` `for` `(map<` `int` `, ` `int` `>::iterator i = h.begin(); ` ` ` `i != h.end(); i++) ` ` ` `cout << i->first << ` `" "` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

1 2 4 5 8 9 10

**Related Post : **

Count number of binary strings without consecutive 1’s

This article is contributed by **Niteesh Kumar**. 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.

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.

## Recommended Posts:

- 1 to n bit numbers with no consecutive 1s in binary representation
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find value of k-th bit in binary representation
- Next greater number than N with exactly one bit different in binary representation of N
- Length of longest consecutive zeroes in the binary representation of a number.
- Find consecutive 1s of length >= n in binary representation of a number
- Find next greater element with no consecutive 1 in it's binary representation
- Maximum number of consecutive 1's in binary representation of all the array elements
- Length of the Longest Consecutive 1s in Binary Representation
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Longest common substring in binary representation of two numbers
- Count numbers have all 1s together in binary representation
- Convert numbers into binary representation and add them without carry
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Sum of consecutive bit differences of first N non-negative integers