Open In App

Count of numbers in range [L, R] with LSB as 0 in their Binary representation

Last Updated : 16 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// C++ implementation of the approach

#include <bits/stdc++.h>
using namespace std;

// Function to return the count
// of required numbers
int countNumbers(int l, int r)
{
    int count = 0;

    for (int i = l; i <= r; i++) {
        // If rightmost bit is 0
        if ((i & 1) == 0) {
            count++;
        }
    }
    // Return the required count
    return count;
}

// Driver code
int main()
{
    int l = 10, r = 20;

    // Call function countNumbers
    cout << countNumbers(l, r);
    return 0;
}
Java
// Java implementation of the approach
import java.io.*;

class GFG{
    
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
    int count = 0;
    for(int i = l; i <= r; i++) 
    {
        
        // If rightmost bit is 0
        if ((i & 1) == 0)
            count += 1;
    }
    
    // Return the required count
    return count;
}

// Driver code
public static void main(String[] args)
{
    int l = 10, r = 20;
    
    // Call function countNumbers
    System.out.println(countNumbers(l, r));
}
}

// This code is contributed by MuskanKalra1
Python3
# Python3 implementation of the approach

# Function to return the count
# of required numbers
def countNumbers(l, r):
    
    count = 0
    
    for i in range(l, r + 1):
        
        # If rightmost bit is 0
        if ((i & 1) == 0):
            count += 1
            
    # Return the required count
    return count

# Driver code
l = 10
r = 20

# Call function countNumbers
print(countNumbers(l, r))

# This code is contributed by amreshkumar3
C#
// C# implementation of the approach
using System;

class GFG {

    // Function to return the count
    // of required numbers
    static int countNumbers(int l, int r)
    {
        int count = 0;
        for (int i = l; i <= r; i++) {

            // If rightmost bit is 0
            if ((i & 1) == 0)
                count += 1;
        }

        // Return the required count
        return count;
    }

    // Driver code
    public static void Main()
    {
        int l = 10, r = 20;

        // Call function countNumbers
        Console.WriteLine(countNumbers(l, r));
    }
}

// This code is contributed by subham348.
JavaScript
<script>

// JavaScript implementation of the approach

// Function to return the count
// of required numbers
function countNumbers(l, r)
{
    let count = 0;

    for (let i = l; i <= r; i++) {
        // If rightmost bit is 0
        if ((i & 1) == 0) {
            count++;
        }
    }
    // Return the required count
    return count;
}

// Driver code
    let l = 10, r = 20;

    // Call function countNumbers
    document.write(countNumbers(l, r));

</script>

Output
6

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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

// Function to return the count
// of required numbers
int countNumbers(int l, int r)
{
    // Count of numbers in range
    // which are divisible by 2
    return ((r / 2) - (l - 1) / 2);
}

// Driver code
int main()
{
    int l = 10, r = 20;
    cout << countNumbers(l, r);
    return 0;
}
Java
// Java implementation of the approach
import java.io.*;

class GFG{
    
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
    
    // Count of numbers in range
    //  which are divisible by 2
    return ((r / 2) - (l - 1) / 2);
}

// Driver Code
public static void main(String[] args)
{
    int l = 10;
    int r = 20;
    
    System.out.println(countNumbers(l, r));
}
}

// This code is contributed by MuskanKalra1
Python3
# Python3 implementation of the approach

# Function to return the count
# of required numbers
def countNumbers(l, r):

    # Count of numbers in range
    #  which are divisible by 2
    return ((r // 2) - (l - 1) // 2)

# Driver code
l = 10
r = 20

print(countNumbers(l, r))

# This code is contributed by amreshkumar3
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;

class GFG{

// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
  
    // Count of numbers in range
    // which are divisible by 2
    return ((r / 2) - (l - 1) / 2);
}

// Driver code
public static void Main()
{
    int l = 10, r = 20;
    Console.Write(countNumbers(l, r));
}
}

// This code is contributed by SURENDRA_GANGWAR.
JavaScript
<script>

// Javascript implementation of the approach

// Function to return the count
// of required numbers
function countNumbers(l, r)
{
    
    // Count of numbers in range
    // which are divisible by 2
    return(parseInt(r / 2) - 
          parseInt((l - 1) / 2));
}

// Driver code
let l = 10, r = 20;

document.write(countNumbers(l, r));

// This code is contributed by subhammahato348

</script>

Output
6

Time Complexity: O(1) 
Auxiliary Space: O(1)


Next Article

Similar Reads