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:
- Odd numbers always end with 1 in their binary representation.
- Every number has the starting bit as 1 in their binary representation.
- Hence, the problem reduces to finding the count of odd numbers in the given range.
Follow the steps below to solve the problem:
- Find the count of odd numbers in the range [1, R] and store it in a variable, say X.
- Similarly, find the count of odd numbers in the range [1, L – 1]. Let it be Y.
- Print X – Y as the answer.
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> |
12
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!