Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmCount numbers from a given range having same first and last digits...

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

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:

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)

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 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments