Related Articles

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

• Difficulty Level : Hard
• Last Updated : 22 Mar, 2021

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

 `// C++ implementation to print numbers in the range``// 1 to n having bits in alternate pattern``#include ` `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;``}`

## Java

 `// 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)>`

## Python3

 `# 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".`

## C#

 `// 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.`

## PHP

 ``

## Javascript

 ``

Output:

`1 2 5 10 21 42`

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up