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

1) There will be 2^{n} numbers with number of bits from 1 to n.

2) Iterate through all 2^{n} numbers. For every number check if it contains consecutive set bits or not. To check, we do bit wise and of current number i and left shifted i. If the bitwise and contains a non-zero bit (or its value is non-zero), then given number doesn’t contain consecutive set bits.

## C++

`// Print all numbers upto n bits ` `// with no consecutive set bits. ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `void` `printNonConsecutive(` `int` `n) ` `{ ` ` ` `// Let us first compute ` ` ` `// 2 raised to power n. ` ` ` `int` `p = (1 << n); ` ` ` ` ` `// loop 1 to n to check ` ` ` `// all the numbers ` ` ` `for` `(` `int` `i = 1; i < p; i++) ` ` ` ` ` `// A number i doesn't contain ` ` ` `// consecutive set bits if ` ` ` `// bitwise and of i and left ` ` ` `// shifted i do't contain a ` ` ` `// commons set bit. ` ` ` `if` `((i & (i << 1)) == 0) ` ` ` `cout << i << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `printNonConsecutive(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Code to Print all numbers upto ` `// n bits with no consecutive set bits. ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `printNonConsecutive(` `int` `n) ` ` ` `{ ` ` ` `// Let us first compute ` ` ` `// 2 raised to power n. ` ` ` `int` `p = (` `1` `<< n); ` ` ` ` ` `// loop 1 to n to check ` ` ` `// all the numbers ` ` ` `for` `(` `int` `i = ` `1` `; i < p; i++) ` ` ` ` ` `// A number i doesn't contain ` ` ` `// consecutive set bits if ` ` ` `// bitwise and of i and left ` ` ` `// shifted i do't contain a ` ` ` `// commons set bit. ` ` ` `if` `((i & (i << ` `1` `)) == ` `0` `) ` ` ` `System.out.print(i + ` `" "` `); ` ` ` ` ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `printNonConsecutive(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Mr. Somesh Awasthi ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print all numbers upto ` `# n bits with no consecutive set bits. ` ` ` `def` `printNonConsecutive(n): ` ` ` ` ` `# Let us first compute ` ` ` `# 2 raised to power n. ` ` ` `p ` `=` `(` `1` `<< n) ` ` ` ` ` `# loop 1 to n to check ` ` ` `# all the numbers ` ` ` `for` `i ` `in` `range` `(` `1` `, p): ` ` ` ` ` `# A number i doesn't contain ` ` ` `# consecutive set bits if ` ` ` `# bitwise and of i and left ` ` ` `# shifted i do't contain a ` ` ` `# common set bit. ` ` ` `if` `((i & (i << ` `1` `)) ` `=` `=` `0` `): ` ` ` `print` `(i, end ` `=` `" "` `) ` ` ` `# Driver code ` `n ` `=` `3` `printNonConsecutive(n) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# Code to Print all numbers upto ` `// n bits with no consecutive set bits. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `printNonConsecutive(` `int` `n) ` ` ` `{ ` ` ` `// Let us first compute ` ` ` `// 2 raised to power n. ` ` ` `int` `p = (1 << n); ` ` ` ` ` `// loop 1 to n to check ` ` ` `// all the numbers ` ` ` `for` `(` `int` `i = 1; i < p; i++) ` ` ` ` ` `// A number i doesn't contain ` ` ` `// consecutive set bits if ` ` ` `// bitwise and of i and left ` ` ` `// shifted i do't contain a ` ` ` `// commons set bit. ` ` ` `if` `((i & (i << 1)) == 0) ` ` ` `Console.Write(i + ` `" "` `); ` ` ` ` ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` `printNonConsecutive(n); ` ` ` `} ` `} ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Print all numbers upto n bits ` `// with no consecutive set bits. ` ` ` `function` `printNonConsecutive(` `$n` `) ` `{ ` ` ` ` ` `// Let us first compute ` ` ` `// 2 raised to power n. ` ` ` `$p` `= (1 << ` `$n` `); ` ` ` ` ` `// loop 1 to n to check ` ` ` `// all the numbers ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$p` `; ` `$i` `++) ` ` ` ` ` `// A number i doesn't contain ` ` ` `// consecutive set bits if ` ` ` `// bitwise and of i and left ` ` ` `// shifted i do't contain a ` ` ` `// commons set bit. ` ` ` `if` `((` `$i` `& (` `$i` `<< 1)) == 0) ` ` ` `echo` `$i` `. ` `" "` `; ` `} ` ` ` ` ` `// Driver code ` ` ` `$n` `= 3; ` ` ` `printNonConsecutive(` `$n` `); ` ` ` `// This code is contributed by Sam007 ` `?> ` |

*chevron_right*

*filter_none*

Complexity O(2^n) because ‘for’ loop is run 2^n time.

Output:

1 2 4 5

This article is contributed by **Devanshu Agarwal**. 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.
- Length of the Longest Consecutive 1s in Binary Representation
- Find consecutive 1s of length >= n in binary representation of a number
- Maximum number of consecutive 1's in binary representation of all the array elements
- Length of longest consecutive zeroes in the binary representation of a number.
- Convert numbers into binary representation and add them without carry
- Fibbinary Numbers (No consecutive 1s in binary)
- Fibbinary Numbers (No consecutive 1s in binary) - O(1) Approach
- Maximum 0's between two immediate 1's in binary representation
- XOR counts of 0s and 1s in binary representation
- Find value of k-th bit in binary representation
- Binary representation of a given number
- Largest number with binary representation is m 1's and m-1 0's
- Maximum distance between two 1's in Binary representation of N
- Next greater number than N with exactly one bit different in binary representation of N
- Binary representation of previous number
- Prime Number of Set Bits in Binary Representation | Set 1
- Decimal representation of given binary string is divisible by 20 or not
- Decimal representation of given binary string is divisible by 10 or not
- Check if binary representation of a number is palindrome