Print numbers having first and last bits as the only set bits
Given a positive integer n. The problem is to print numbers in the range 1 to n having first and last bits as the only set bits.
Examples:
Input : n = 10 Output : 1 3 5 9 (1)10 = (1)2. (3)10 = (11)2. (5)10 = (101)2. (9)10 = (1001)2
Naive Approach: Print “1”. Now for i = 3 to n, check if (i-1) is a Perfect power of two or not. If true then print i.
C++
// C++ implementation to print numbers in the range 1 to n // having first and last bits as the only set bits #include <bits/stdc++.h> using namespace std; typedef unsigned long long int ull; // function to check whether 'n' // is a power of 2 or not bool powerOfTwo(ull n) { return (!(n & n-1)); } // fuction to print numbers in the range 1 to n having // first and last bits as the only set bits void printNumWithFirstLastBitsSet(ull n) { ull i = 1; // first number is '1' cout << i << " "; // generating all the numbers for (i = 3; i <= n; i++) // if true, then print 'i' if (powerOfTwo(i-1)) cout << i << " "; } // Driver program to test above int main() { ull n = 10; printNumWithFirstLastBitsSet(n); return 0; } |
Java
// Naive approach // Java implementation to print // numbers in the range 1 to n // having first and last bits as // the only set bits import java.io.*; class GFG { // function to check whether 'n' // is a power of 2 or not static Boolean powerOfTwo(long n) { return (!((n & n-1) != 0)); } // fuction to print numbers in the // range 1 to n having first and // last bits as the only set bits static void printNumWithFirstLastBitsSet(long n) { long i = 1; // first number is '1' System.out.print( i + " "); // generating all the numbers for (i = 3; i <= n; i++) // if true, then print 'i' if (powerOfTwo(i - 1)) System.out.print(i + " "); } // Driver function public static void main (String[] args) { long n = 10l; printNumWithFirstLastBitsSet(n); } } //This code is contributed by Gitanjali. |
Python3
# Python implementation to print # numbers in the range 1 to n # having first and last bits # as the only set bits import math # function to check whether 'n' # is a power of 2 or not def powerOfTwo(n): re = (n & n - 1) return (re == 0) # fuction to print numbers # in the range 1 to n having # first and last bits as # the only set bits def printNumWithFirstLastBitsSet(n): i = 1 # first number is '1' print ( i, end = " ") # generating all the numbers for i in range(3, n + 1): # if true, then print 'i' if (powerOfTwo(i - 1)): print ( i, end = " ") # driver function n = 10printNumWithFirstLastBitsSet(n) # This code is contributed by Gitanjali. |
C#
// Naive approach // C# implementation to print // numbers in the range 1 to n // having first and last bits as // the only set bits using System; class GFG { // function to check whether 'n' // is a power of 2 or not static Boolean powerOfTwo(long n) { return (!((n & n-1) != 0)); } // fuction to print numbers in the // range 1 to n having first and // last bits as the only set bits static void printNumWithFirstLastBitsSet(long n) { long i = 1; // first number is '1' Console.Write( i + " "); // generating all the numbers for (i = 3; i <= n; i++) // if true, then print 'i' if (powerOfTwo(i - 1)) Console.Write(i + " "); } // Driver function public static void Main () { long n = 10L; printNumWithFirstLastBitsSet(n); } } // This code is contributed by Vt_m. |
PHP
<?php // php implementation to print // numbers in the range 1 to n // having first and last bits // as the only set bits // function to check whether 'n' // is a power of 2 or not function powerOfTwo($n) { return (!($n & $n - 1)); } // fuction to print numbers in // the range 1 to n having // first and last bits as // the only set bits function printNumWithFirstLastBitsSet($n) { $i = 1; // first number is '1' echo $i." "; // generating all the numbers for ($i = 3; $i <= $n; $i++) // if true, then print 'i' if (powerOfTwo($i - 1)) echo $i." "; } // Driver Code $n = 10; printNumWithFirstLastBitsSet($n); // This Code is contributed by mits ?> |
Output:
1 3 5 9
Time Complexity: O(n).
Efficient Approach: Print “1”. Now one by one generate perfect power of two (except ‘1’) with the help of bitwise left shift operation. Bitwise xor these numbers with 1 and if result is in the range print them else stop.
C++
// C++ implementation to print numbers in the range 1 to n // having first and last bits as the only set bits #include <bits/stdc++.h> using namespace std; typedef unsigned long long int ull; // fuction to print numbers in the range 1 to n having // first and last bits as the only set bits void printNumWithFirstLastBitsSet(ull n) { ull power_2 = 1, num; // first number is '1' cout << power_2 << " "; while (1) { // obtaining next perfect power of 2 power_2 <<= 1; // toggling the last bit to convert // it to as set bit num = power_2 ^ 1; // if out of range then break; if (n < num) break; // display cout << num << " "; } } // Driver program to test above int main() { ull n = 10; printNumWithFirstLastBitsSet(n); return 0; } |
Java
// efficient approach Java implementation // to print numbers in the range 1 to n // having first and last bits as the only set bits import java.io.*; class GFG { // fuction to print numbers in // the range 1 to n having first and // last bits as the only set bits static void prNumWithFirstLastBitsSet(long n) { long power_2 = 1, num; // first number is '1' System.out.print(power_2 + " "); while (true) { // obtaining next perfect power of 2 power_2 <<= 1; // toggling the last bit to // convert it to as set bit num = power_2 ^ 1; // if out of range then break; if (n < num) break; // display System.out.print(num + " "); } } public static void main (String[] args) { long n = 10; prNumWithFirstLastBitsSet(n); } } // This code is contributed by Gitanjali. |
Python3
# Python3 implementation to # pr numbers in the range # 1 to n having first and # last bits as the only set bits # fuction to pr numbers in the # range 1 to n having first and # last bits as the only set bits def prNumWithFirstLastBitsSet(n): power_2 = 1 # first number is '1' print ( power_2, end = ' ') while (1): # obtaining next perfect # power of 2 power_2 <<= 1 # toggling the last bit to # convert it to as set bit num = power_2 ^ 1 # if out of range then break; if (n < num): break # display print ( num, end = ' ') # Driver program n = 10; prNumWithFirstLastBitsSet(n) # This code is contributed by saloni1297 |
C#
// efficient approach C# implementation // to print numbers in the range 1 to n // having first and last bits as the only set bits using System; class GFG { // fuction to print numbers in // the range 1 to n having first and // last bits as the only set bits static void prNumWithFirstLastBitsSet(long n) { long power_2 = 1, num; // first number is '1' Console.Write(power_2 + " "); while (true) { // obtaining next perfect power of 2 power_2 <<= 1; // toggling the last bit to // convert it to as set bit num = power_2 ^ 1; // if out of range then break; if (n < num) break; // display Console.Write(num + " "); } } // Driver code public static void Main () { long n = 10; prNumWithFirstLastBitsSet(n); } } // This code is contributed by vt_m. |
PHP
<?php // php implementation to print // numbers in the range 1 to n // having first and last bits // as the only set bits // fuction to print numbers in // the range 1 to n having // first and last bits as // the only set bits function printNumWithFirstLastBitsSet($n) { $power_2 = 1; // first number is '1' echo $power_2." "; while (1) { // obtaining next perfect // power of 2 $power_2 <<= 1; // toggling the last // bit to convert // it to as set bit $num = $power_2 ^ 1; // if out of range // then break; if ($n < $num) break; // display echo $num." "; } } // Driver code $n = 10; printNumWithFirstLastBitsSet($n); // This code is contributed by mits ?> |
Output:
1 3 5 9
Recommended Posts:
- Print first n numbers with exactly two set bits
- Print numbers in the range 1 to n having bits in alternate pattern
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Sum of numbers with exactly 2 bits set
- Print the number of set bits in each node of a Binary Tree
- Count total set bits in all numbers from 1 to n
- Count total set bits in all numbers from 1 to n | Set 2
- Check if bits in range L to R of two numbers are complement of each other or not
- Alternate bits of two numbers to create a new number
- Maximum sum by adding numbers with same number of set bits
- Closest (or Next) smaller and greater numbers with same number of set bits
- Python map function | Count total set bits in all numbers from 1 to n
- First and Last Three Bits
- Toggle the last m bits
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



