Number with set bits only between L-th and R-th index
Given L and R. The task is to find the number in whose binary representation all bits between the L-th and R-th index are set and the rest of the bits are unset. The binary representation is 32 bits.
Examples:
Input: L = 2, R = 5
Output: 60
Explanation: The binary representation is
0..0111100 => 60
Input: L = 1, R = 3
Output: 14
Explanation: The binary representation is
0..01110 => 14
Naive Approach: The naive approach to finding the number is to iterate from i = L to i = R and calculate the addition of all the powers of 2i.
The program below illustrates the naive approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getInteger( int L, int R)
{
int number = 0;
for ( int i = L; i <= R; i++)
number += pow (2, i);
return number;
}
int main()
{
int L = 2, R = 5;
cout << getInteger(L, R);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int getInteger( int L,
int R)
{
int number = 0 ;
for ( int i = L; i <= R; i++)
number += Math.pow( 2 , i);
return number;
}
public static void main (String[] args)
{
int L = 2 , R = 5 ;
System.out.println(getInteger(L, R));
}
}
|
Python3
from math import pow
def getInteger(L, R):
number = 0
for i in range (L, R + 1 , 1 ):
number + = pow ( 2 , i)
return number
if __name__ = = '__main__' :
L = 2
R = 5
print ( int (getInteger(L, R)))
|
C#
using System;
class GFG
{
static int getInteger( int L,
int R)
{
int number = 0;
for ( int i = L; i <= R; i++)
number += ( int )Math.Pow(2, i);
return number;
}
public static void Main ()
{
int L = 2, R = 5;
Console.Write(getInteger(L, R));
}
}
|
PHP
<?php
function getInteger( $L , $R )
{
$number = 0;
for ( $i = $L ; $i <= $R ; $i ++)
$number += pow(2, $i );
return $number ;
}
$L = 2;
$R = 5;
echo getInteger( $L , $R );
?>
|
Javascript
<script>
function getInteger(L, R)
{
var number = 0;
for ( var i = L; i <= R; i++)
number += Math.pow(2, i);
return number;
}
var L = 2, R = 5;
document.write( getInteger(L, R));
</script>
|
Time Complexity: O(R2), considering the time complexity of the pow() method.
Auxiliary Space: O(1)
An efficient approach is to compute the number with all (R) bits set from the right and subtract the number with all (L-1) bits set from the right to get the required number.
1. Compute the number which has all R set bits from the right using the below formula.
(1 << (R+1)) - 1.
2. Subtract the number which has all (L-1) set bits from the right.
(1<<L) - 1
Hence, computing ((1<<(R+1))-1)-((1<<L)-1), we get the final formula as:
(1<<(R+1))-(1<<L)
The program below illustrates the efficient approach:
C++
#include <bits/stdc++.h>
using namespace std;
int setbitsfromLtoR( int L, int R)
{
return (1 << (R + 1)) - (1 << L);
}
int main()
{
int L = 2, R = 5;
cout << setbitsfromLtoR(L, R);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int setbitsfromLtoR( int L,
int R)
{
return ( 1 << (R + 1 )) -
( 1 << L);
}
public static void main (String[] args)
{
int L = 2 , R = 5 ;
System.out.println(setbitsfromLtoR(L, R));
}
}
|
Python3
def setbitsfromLtoR(L, R):
return (( 1 << (R + 1 )) -
( 1 << L))
L = 2
R = 5
print (setbitsfromLtoR(L, R))
|
C#
using System;
class GFG
{
static int setbitsfromLtoR( int L,
int R)
{
return (1 << (R + 1)) -
(1 << L);
}
public static void Main ()
{
int L = 2, R = 5;
Console.WriteLine(setbitsfromLtoR(L, R));
}
}
|
PHP
<?php
function setbitsfromLtoR( $L , $R )
{
return (1 << ( $R + 1)) -
(1 << $L );
}
$L = 2;
$R = 5;
echo setbitsfromLtoR( $L , $R );
?>
|
Javascript
<script>
function setbitsfromLtoR(L, R)
{
return (1 << (R + 1)) - (1 << L);
}
var L = 2, R = 5;
document.write( setbitsfromLtoR(L, R));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...