Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Count numbers from a given range having same first and last digits in their binary representation

  • Difficulty Level : Basic
  • Last Updated : 23 Apr, 2021

Given two integers L and R, the task is to find the count of numbers in the range [L, R] whose first and last digit in the binary representation are the same.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: L = 1, R = 5
Output: 3
Explanation:
(1)10 = (1)2
(2)10 = (10)2
(3)10 = (11)2
(4)10 = (100)2
(5)10 = (101)2
Therefore, 1, 3, 5 has the same first and last digits in their binary representation.

Input: L = 6, R = 30
Output: 12



Naive Approach: The simplest approach is to iterate over the range L to R and find the binary representation of all the numbers and check for each of them, if the first and the last digit in their binary representation is the same or not. 
Time Complexity: O((R-L)log(R-L))
Auxiliary Space: O(1)

Efficient Approach: The given problem can be solved based on the following observations:

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count numbers in range
// [L, R] having same first and last
// digit in the binary representation
void Count_numbers(int L, int R)
{
    int count = (R - L) / 2;
 
    if (R % 2 != 0 || L % 2 != 0)
        count += 1;
 
    cout << count << endl;
}
 
// Drivers code
int main()
{
 
    // Given range [L, R]
    int L = 6, R = 30;
 
    // Function Call
    Count_numbers(L, R);
}

Java




// Java program to implement
// the above approach
import java.util.*;
class GFG
{
        
// Function to count numbers in range
// [L, R] having same first and last
// digit in the binary representation
static void Count_numbers(int L, int R)
{
    int count = (R - L) / 2;
    if (R % 2 != 0 || L % 2 != 0)
        count += 1;
    System.out.print(count);
}
    
// Driver code
public static void main(String[] args)
{
    // Given range [L, R]
    int L = 6, R = 30;
 
    // Function Call
    Count_numbers(L, R);
}
}
 
// This code is contributed by sanjoy_62

Python3




# Python program for the above approach
 
# Function to count numbers in range
# [L, R] having same first and last
# digit in the binary representation
def Count_numbers(L, R) :  
    count = (R - L) // 2
    if (R % 2 != 0 or L % 2 != 0) :
        count += 1
    print(count)
 
# Drivers code
 
# Given range [L, R]
L = 6
R = 30
 
# Function Call
Count_numbers(L, R)
 
# This code is contributed by code_hunt.

C#




// C# program to implement
// the above approach
using System;
class GFG
{
        
// Function to count numbers in range
// [L, R] having same first and last
// digit in the binary representation
static void Count_numbers(int L, int R)
{
    int count = (R - L) / 2;
    if (R % 2 != 0 || L % 2 != 0)
        count += 1;
    Console.Write(count);
}
    
// Driver code
public static void Main(String[] args)
{
   
    // Given range [L, R]
    int L = 6, R = 30;
 
    // Function Call
    Count_numbers(L, R);
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
// javascript program to implement
// the above approach
 
    // Function to count numbers in range
    // [L, R] having same first and last
    // digit in the binary representation
    function Count_numbers(L , R)
    {
        var count = (R - L) / 2;
        if (R % 2 != 0 || L % 2 != 0)
            count += 1;
        document.write(count);
    }
 
    // Driver code
     
        // Given range [L, R]
        var L = 6, R = 30;
 
        // Function Call
        Count_numbers(L, R);
 
// This code is contributed by Rajput-Ji
</script>
Output: 
12

 

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :