Print numbers having first and last bits as the only set bits
Last Updated :
29 Mar, 2024
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++
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
bool powerOfTwo(ull n)
{
return (!(n & n-1));
}
void printNumWithFirstLastBitsSet(ull n)
{
ull i = 1;
cout << i << " " ;
for (i = 3; i <= n; i++)
if (powerOfTwo(i-1))
cout << i << " " ;
}
int main()
{
ull n = 10;
printNumWithFirstLastBitsSet(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static Boolean powerOfTwo( long n)
{
return (!((n & n- 1 ) != 0 ));
}
static void printNumWithFirstLastBitsSet( long n)
{
long i = 1 ;
System.out.print( i + " " );
for (i = 3 ; i <= n; i++)
if (powerOfTwo(i - 1 ))
System.out.print(i + " " );
}
public static void main (String[] args) {
long n = 10l;
printNumWithFirstLastBitsSet(n);
}
}
|
Python3
import math
def powerOfTwo(n):
re = (n & n - 1 )
return (re = = 0 )
def printNumWithFirstLastBitsSet(n):
i = 1
print ( i, end = " " )
for i in range ( 3 , n + 1 ):
if (powerOfTwo(i - 1 )):
print ( i, end = " " )
n = 10
printNumWithFirstLastBitsSet(n)
|
C#
using System;
class GFG {
static Boolean powerOfTwo( long n)
{
return (!((n & n-1) != 0));
}
static void printNumWithFirstLastBitsSet( long n)
{
long i = 1;
Console.Write( i + " " );
for (i = 3; i <= n; i++)
if (powerOfTwo(i - 1))
Console.Write(i + " " );
}
public static void Main ()
{
long n = 10L;
printNumWithFirstLastBitsSet(n);
}
}
|
PHP
<?php
function powerOfTwo( $n )
{
return (!( $n & $n - 1));
}
function printNumWithFirstLastBitsSet( $n )
{
$i = 1;
echo $i . " " ;
for ( $i = 3; $i <= $n ; $i ++)
if (powerOfTwo( $i - 1))
echo $i . " " ;
}
$n = 10;
printNumWithFirstLastBitsSet( $n );
?>
|
Javascript
<script>
function powerOfTwo(n)
{
return (!(n & n-1));
}
function printNumWithFirstLastBitsSet(n)
{
let i = 1;
document.write(i + " " );
for (i = 3; i <= n; i++)
if (powerOfTwo(i-1))
document.write(i + " " );
}
let n = 10;
printNumWithFirstLastBitsSet(n);
</script>
|
Output:
1 3 5 9
Time Complexity : O(n)
Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
void printNumWithFirstLastBitsSet(ull n)
{
ull power_2 = 1, num;
cout << power_2 << " " ;
while (1)
{
power_2 <<= 1;
num = power_2 ^ 1;
if (n < num)
break ;
cout << num << " " ;
}
}
int main()
{
ull n = 10;
printNumWithFirstLastBitsSet(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void prNumWithFirstLastBitsSet( long n)
{
long power_2 = 1 , num;
System.out.print(power_2 + " " );
while ( true )
{
power_2 <<= 1 ;
num = power_2 ^ 1 ;
if (n < num)
break ;
System.out.print(num + " " );
}
}
public static void main (String[] args) {
long n = 10 ;
prNumWithFirstLastBitsSet(n);
}
}
|
Python3
def prNumWithFirstLastBitsSet(n):
power_2 = 1
print ( power_2, end = ' ' )
while ( 1 ):
power_2 << = 1
num = power_2 ^ 1
if (n < num):
break
print ( num, end = ' ' )
n = 10 ;
prNumWithFirstLastBitsSet(n)
|
C#
using System;
class GFG {
static void prNumWithFirstLastBitsSet( long n)
{
long power_2 = 1, num;
Console.Write(power_2 + " " );
while ( true )
{
power_2 <<= 1;
num = power_2 ^ 1;
if (n < num)
break ;
Console.Write(num + " " );
}
}
public static void Main ()
{
long n = 10;
prNumWithFirstLastBitsSet(n);
}
}
|
PHP
<?php
function printNumWithFirstLastBitsSet( $n )
{
$power_2 = 1;
echo $power_2 . " " ;
while (1)
{
$power_2 <<= 1;
$num = $power_2 ^ 1;
if ( $n < $num )
break ;
echo $num . " " ;
}
}
$n = 10;
printNumWithFirstLastBitsSet( $n );
?>
|
Javascript
<script>
function printNumWithFirstLastBitsSet(n)
{
var power_2 = 1, num;
document.write(power_2 + " " );
while ( true )
{
power_2 <<= 1;
num = power_2 ^ 1;
if (n < num)
break ;
document.write(num + " " );
}
}
var n = 10;
printNumWithFirstLastBitsSet(n);
</script>
|
Output:
1 3 5 9
Time Complexity : O(logn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...