Given two integers L and R, the task is to count the numbers having only one unset bit in the range [L, R].
Examples:
Input: L = 4, R = 9
Output: 2
Explanation:
The binary representation of all numbers in the range [4, 9] are
4 = (100)2
5 = (101)2
6 = (110)2
7 = (111)2
8 = (1000)2
9 = (1001)2
Out of all the above numbers, only 5 and 6 have exactly one unset bit.
Therefore, the required count is 2.
Input: L = 10, R = 13
Output: 2
Explanation:
The binary representations of all numbers in the range [10, 13]
10 = (1010)2
11 = (1011)2
12 = (1100)2
13 = (1101)2
Out of all the above numbers, only 11 and 13 have exactly one unset bit.
Therefore, the required count is 2.
Naive Approach: The simplest approach is to check every number in the range [L, R] whether it has exactly one unset bit or not. For every such number, increment the count. After traversing the entire range, print the value of count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_numbers( int L, int R)
{
int ans = 0;
for ( int n = L; n <= R; n++) {
int no_of_bits = log2(n) + 1;
int no_of_set_bits
= __builtin_popcount(n);
if (no_of_bits
- no_of_set_bits
== 1) {
ans++;
}
}
return ans;
}
int main()
{
int L = 4, R = 9;
cout << count_numbers(L, R);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int count_numbers( int L,
int R)
{
int ans = 0 ;
for ( int n = L; n <= R; n++)
{
int no_of_bits = ( int ) (Math.log(n) + 1 );
int no_of_set_bits = Integer.bitCount(n);
if (no_of_bits - no_of_set_bits == 1 )
{
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
int L = 4 , R = 9 ;
System.out.print(count_numbers(L, R));
}
}
|
Python3
from math import log2
def count_numbers(L, R):
ans = 0
for n in range (L, R + 1 ):
no_of_bits = int (log2(n) + 1 )
no_of_set_bits = bin (n).count( '1' )
if (no_of_bits - no_of_set_bits = = 1 ):
ans + = 1
return ans
if __name__ = = '__main__' :
L = 4
R = 9
print (count_numbers(L, R))
|
C#
using System;
class GFG{
static int count_numbers( int L,
int R)
{
int ans = 0;
for ( int n = L; n <= R; n++)
{
int no_of_bits = ( int )(Math.Log(n) + 1);
int no_of_set_bits = bitCount(n);
if (no_of_bits - no_of_set_bits == 1)
{
ans++;
}
}
return ans;
}
static int bitCount( long x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
public static void Main(String[] args)
{
int L = 4, R = 9;
Console.Write(count_numbers(L, R));
}
}
|
Javascript
<script>
function count_numbers(L, R)
{
let ans = 0;
for (let n = L; n <= R; n++)
{
let no_of_bits = Math.floor(Math.log(n) + 1);
let no_of_set_bits = bitCount(n);
if (no_of_bits - no_of_set_bits == 1)
{
ans++;
}
}
return ans;
}
function bitCount(x)
{
let setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
let L = 4, R = 9;
document.write(count_numbers(L, R));
</script>
|
Time Complexity: O(N*Log R)
Auxiliary Space: O(1)
Efficient Approach: Since the maximum number of bits is at most log R and the number should contain exactly one zero in its binary representation, fix one position in [0, log R] as the unset bit and set all other bits and increment count if the generated number is within the given range.
Follow the steps below to solve the problem:
- Loop over all possible positions of unset bit, say zero_bit, in the range [0, log R]
- Set all bits before zero_bit.
- Iterate over the range j = zero_bit + 1 to log R and set the bit at position j and increment count if the number formed is within the range [L, R].
- Print the count after the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_numbers( int L, int R)
{
int ans = 0;
int LogR = log2(R) + 1;
for ( int zero_bit = 0;
zero_bit < LogR; zero_bit++) {
int cur = 0;
for ( int j = 0; j < zero_bit; j++) {
cur |= (1LL << j);
}
for ( int j = zero_bit + 1;
j < LogR; j++) {
cur |= (1LL << j);
if (cur >= L && cur <= R) {
ans++;
}
}
}
return ans;
}
int main()
{
long long L = 4, R = 9;
cout << count_numbers(L, R);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int count_numbers( int L, int R)
{
int ans = 0 ;
int LogR = ( int ) (Math.log(R) + 1 );
for ( int zero_bit = 0 ; zero_bit < LogR;
zero_bit++)
{
int cur = 0 ;
for ( int j = 0 ; j < zero_bit; j++)
{
cur |= (1L << j);
}
for ( int j = zero_bit + 1 ;
j < LogR; j++)
{
cur |= (1L << j);
if (cur >= L && cur <= R)
{
ans++;
}
}
}
return ans;
}
public static void main(String[] args)
{
int L = 4 , R = 9 ;
System.out.print(count_numbers(L, R));
}
}
|
Python3
import math
def count_numbers(L, R):
ans = 0 ;
LogR = ( int )(math.log(R) + 1 );
for zero_bit in range (LogR):
cur = 0 ;
for j in range (zero_bit):
cur | = ( 1 << j);
for j in range (zero_bit + 1 , LogR):
cur | = ( 1 << j);
if (cur > = L and cur < = R):
ans + = 1 ;
return ans;
if __name__ = = '__main__' :
L = 4 ;
R = 9 ;
print (count_numbers(L, R));
|
C#
using System;
public class GFG{
static int count_numbers( int L, int R)
{
int ans = 0;
int LogR = ( int ) (Math.Log(R) + 1);
for ( int zero_bit = 0; zero_bit < LogR;
zero_bit++)
{
int cur = 0;
for ( int j = 0; j < zero_bit; j++)
{
cur |= (1 << j);
}
for ( int j = zero_bit + 1;
j < LogR; j++)
{
cur |= (1 << j);
if (cur >= L && cur <= R)
{
ans++;
}
}
}
return ans;
}
public static void Main(String[] args)
{
int L = 4, R = 9;
Console.Write(count_numbers(L, R));
}
}
|
Javascript
<script>
function count_numbers(L, R)
{
let ans = 0;
let LogR = (Math.log(R) + 1);
for (let zero_bit = 0; zero_bit < LogR;
zero_bit++)
{
let cur = 0;
for (let j = 0; j < zero_bit; j++)
{
cur |= (1 << j);
}
for (let j = zero_bit + 1;
j < LogR; j++)
{
cur |= (1 << j);
if (cur >= L && cur <= R)
{
ans++;
}
}
}
return ans;
}
let L = 4, R = 9;
document.write(count_numbers(L, R));
</script>
|
Time Complexity: O((log R)2)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!