Given two integers L and R. The task is to find the count of all numbers in the range [L, R] whose Least Significant Bit in binary representation is 0.
Examples:
Input: L = 10, R = 20
Output: 6
Input: L = 7, R = 11
Output: 2
Naive approach: The simplest approach is to solve this problem is to check for every number in the range [L, R], if Least Significant Bit in binary representation is 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countNumbers( int l, int r)
{
int count = 0;
for ( int i = l; i <= r; i++) {
if ((i & 1) == 0) {
count++;
}
}
return count;
}
int main()
{
int l = 10, r = 20;
cout << countNumbers(l, r);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int countNumbers( int l, int r)
{
int count = 0 ;
for ( int i = l; i <= r; i++)
{
if ((i & 1 ) == 0 )
count += 1 ;
}
return count;
}
public static void main(String[] args)
{
int l = 10 , r = 20 ;
System.out.println(countNumbers(l, r));
}
}
|
Python3
def countNumbers(l, r):
count = 0
for i in range (l, r + 1 ):
if ((i & 1 ) = = 0 ):
count + = 1
return count
l = 10
r = 20
print (countNumbers(l, r))
|
C#
using System;
class GFG {
static int countNumbers( int l, int r)
{
int count = 0;
for ( int i = l; i <= r; i++) {
if ((i & 1) == 0)
count += 1;
}
return count;
}
public static void Main()
{
int l = 10, r = 20;
Console.WriteLine(countNumbers(l, r));
}
}
|
Javascript
<script>
function countNumbers(l, r)
{
let count = 0;
for (let i = l; i <= r; i++) {
if ((i & 1) == 0) {
count++;
}
}
return count;
}
let l = 10, r = 20;
document.write(countNumbers(l, r));
</script>
|
Time Complexity: O(r – l)
Auxiliary Space: O(1)
Efficient approach: This problem can be solved by using properties of bits. Only even numbers have rightmost bit as 0. The count can be found using this formula ((R / 2) – (L – 1) / 2) in O(1) time.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countNumbers( int l, int r)
{
return ((r / 2) - (l - 1) / 2);
}
int main()
{
int l = 10, r = 20;
cout << countNumbers(l, r);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int countNumbers( int l, int r)
{
return ((r / 2 ) - (l - 1 ) / 2 );
}
public static void main(String[] args)
{
int l = 10 ;
int r = 20 ;
System.out.println(countNumbers(l, r));
}
}
|
Python3
def countNumbers(l, r):
return ((r / / 2 ) - (l - 1 ) / / 2 )
l = 10
r = 20
print (countNumbers(l, r))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countNumbers( int l, int r)
{
return ((r / 2) - (l - 1) / 2);
}
public static void Main()
{
int l = 10, r = 20;
Console.Write(countNumbers(l, r));
}
}
|
Javascript
<script>
function countNumbers(l, r)
{
return (parseInt(r / 2) -
parseInt((l - 1) / 2));
}
let l = 10, r = 20;
document.write(countNumbers(l, r));
</script>
|
Time Complexity: O(1)
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!
Last Updated :
16 Jul, 2021
Like Article
Save Article