# 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 = 5Output:3Explanation:

(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 = 30Output: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>` |

**Output:**

12

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

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****.**