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
Approach:
- There will be 2n numbers with number of bits from 1 to n.
- Iterate through all 2n numbers. For every number check if it contains consecutive set bits or not. To check, we do bitwise 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 the given number contains consecutive set bits.
Below is the implementation of the above approach:
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 don't contain a         // commons set bit.         if ((i & (i << 1)) == 0)             cout << i << " " ; } Â
// Driver code int main() { Â Â Â Â int n = 3; Â Â Â Â printNonConsecutive(n); Â Â Â Â return 0; } |
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 doesn'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 |
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 don'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. |
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 don'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. |
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 don'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 ?> |
Javascript
<script> Â
// Javascript Code to Print all numbers upto // n bits with no consecutive set bits. Â
function printNonConsecutive(n)         {             // Let us first compute             // 2 raised to power n.             let p = (1 << n); Â
            // loop 1 to n to check             // all the numbers             for (let i = 1; i < p; i++) Â
            // A number i doesn't contain             // consecutive set bits if             // bitwise and of i and left             // shifted i don't contain a             // commons set bit.             if ((i & (i << 1)) == 0)                 document.write(i + " " );                  } Â
// driver program Â
        let n = 3;         printNonConsecutive(n);          </script> |
1 2 4 5
Time Complexity: O(2N)
Auxiliary Space: O(1)
If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!