Print numbers in the range 1 to n having bits in alternate pattern

Given a positive integer n. The problem is to print the numbers in the range 1 to n having bits in alternate pattern. Here alternate pattern means that the set and unset bits in the number occur in alternate order. For example- 5 has an alternate pattern i.e. 101.

Examples:

Input : n = 10
Output : 1 2 5 10

Input : n = 50
Output : 1 2 5 10 21 42



Method 1 (Naive Approach): Generate all the numbers in the range 1 to n and for each generated number check whether it has bits in alternate pattern. Time Complexity is of O(n).

Method 2 (Efficient Approach): Algorithm:

printNumHavingAltBitPatrn(n)
    Initialize curr_num = 1
    print curr_num    
    while (1)
        curr_num <<= 1
    if n < curr_num then
        break
    print curr_num
    curr_num = ((curr_num) << 1) ^ 1    
    if n < curr_num then
        break
    print curr_num    

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to print numbers in the range
// 1 to n having bits in alternate pattern
#include <bits/stdc++.h>
  
using namespace std;
  
// function to print numbers in the range 1 to n
// having bits in alternate pattern
void printNumHavingAltBitPatrn(int n)
{
    // first number having bits in alternate pattern
    int curr_num = 1;
  
    // display
    cout << curr_num << " ";
  
    // loop until n < curr_num
    while (1) {
  
        // generate next number having alternate
        // bit pattern
        curr_num <<= 1;
  
        // if true then break
        if (n < curr_num)
            break;
  
        // display
        cout << curr_num << " ";
  
        // generate next number having alternate
        // bit pattern
        curr_num = ((curr_num) << 1) ^ 1;
  
        // if true then break
        if (n < curr_num)
            break;
  
        // display
        cout << curr_num << " ";
    }
}
  
// Driver program to test above
int main()
{
    int n = 50;
    printNumHavingAltBitPatrn(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to print numbers in the range
// 1 to n having bits in alternate pattern
  
import java.io.*;
import java.util.*;
  
class GFG
{
    public static void printNumHavingAltBitPatrn(int n)
    {
        // first number having bits in alternate pattern
        int curr_num = 1, i = 1;
  
        // display
        System.out.print(curr_num + " ");
  
        // loop until n < curr_num
        while (i!=0)
        {
            i++; 
            // generate next number having alternate
            // bit pattern
            curr_num <<= 1;
  
            // if true then break
            if (n < curr_num)
                break;
  
            // display
            System.out.print(curr_num + " ");
  
            // generate next number having alternate
            // bit pattern
            curr_num = ((curr_num) << 1) ^ 1;
  
            // if true then break
            if (n < curr_num)
                break;
  
            // display
            System.out.print(curr_num + " ");
        }
    }
    public static void main (String[] args)
    {
        int n = 50;
        printNumHavingAltBitPatrn(n);
    }
}
  
// Code Contributed by Mohit Gupta_OMG <(0_o)>

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for count total
# zero in product of array
  
# function to print numbers in the range 
# 1 to nhaving bits in alternate pattern
def printNumHavingAltBitPatrn(n):
      
    # first number having bits in
    # alternate pattern
    curr_num = 1
  
    # display
    print (curr_num)
  
    # loop until n < curr_num
    while (1) :
  
        # generate next number having 
        # alternate bit pattern
        curr_num = curr_num << 1;
  
        # if true then break
        if (n < curr_num):
            break;
  
        # display
        print( curr_num )
  
        # generate next number having 
        # alternate bit pattern
        curr_num = ((curr_num) << 1) ^ 1;
  
        # if true then break
        if (n < curr_num):
            break
  
        # display
        print( curr_num )
  
# Driven code
n = 50
printNumHavingAltBitPatrn(n)
      
# This code is contributed by "rishabh_jain".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to print numbers in the range
// 1 to n having bits in alternate pattern
using System;
  
class GFG {
      
    // function to print numbers in the range 1 to n
    // having bits in alternate pattern
    public static void printNumHavingAltBitPatrn(int n)
    {
          
        // first number having bits in alternate pattern
        int curr_num = 1, i = 1;
  
        // display
        Console.Write(curr_num + " ");
  
        // loop until n < curr_num
        while (i!=0)
        {
              
            // generate next number having alternate
            // bit pattern
            curr_num <<= 1;
  
            // if true then break
            if (n < curr_num)
                break;
  
            // display
            Console.Write(curr_num + " ");
  
            // generate next number having alternate
            // bit pattern
            curr_num = ((curr_num) << 1) ^ 1;
  
            // if true then break
            if (n < curr_num)
                break;
  
            // display
            Console.Write(curr_num + " ");
        }
    }
      
    // Driver code
    public static void Main ()
    {
        int n = 50;
          
        printNumHavingAltBitPatrn(n);
    }
}
  
// This code is contributed by Sam007.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php implementation to print 
// numbers in the range
// 1 to n having bits in 
// alternate pattern
  
  
// function to print numbers
// in the range 1 to n
// having bits in alternate 
// pattern
function printNumHavingAltBitPatrn($n)
{
      
    // first number having bits
    // in alternate pattern
    $curr_num = 1;
  
    // display
    echo $curr_num." ";
  
    // loop until n < curr_num
    while (1)
    {
  
        // generate next number 
        // having alternate
        // bit pattern
        $curr_num <<= 1;
  
        // if true then break
        if ($n < $curr_num)
            break;
  
        // display
        echo $curr_num." ";
  
        // generate next number 
        // having alternate
        // bit pattern
        $curr_num = (($curr_num) << 1) ^ 1;
  
        // if true then break
        if ($n < $curr_num)
            break;
  
        // display
        echo $curr_num." ";
    }
}
  
    // Driver code
    $n = 50;
    printNumHavingAltBitPatrn($n);
  
// This code is contributed by mits 
?>

chevron_right



Output:

1 2 5 10 21 42


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.