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
// 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; } |
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
<?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 ?> |
Output:
1 2 5 10 21 42
Recommended Posts:
- How to swap two numbers without using a temporary variable?
- Write an Efficient C Program to Reverse Bits of a Number
- Count set bits in an integer
- Count number of bits to be flipped to convert A to B
- Rotate bits of a number
- Next higher number with same number of set bits
- Program to count number of set bits in an (big) array
- Count total set bits in all numbers from 1 to n
- Add two numbers without using arithmetic operators
- Swap bits in a given number
- Swap all odd and even bits
- Russian Peasant (Multiply two numbers using bitwise operators)
- Toggle all the bits of a number except k-th bit.
- Subtract two numbers without using arithmetic operators
- Copy set bits in a range
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