Given two integers **L **and **R** denoting the starting and end values of a range, the task is to count all numbers in that range whose all digit are same, like 1, 22, 444, 3333, etc.

**Example:**

Input:L = 12, R = 68

Output:5

Explanation:

{ 22, 33, 44, 55, 66} are the numbers with same digits in the given range.

Input:L = 1, R = 32

Output:11

**Naive Approach:** Iterate through all the numbers from L to R and for each number, check if it has all its digits same. If yes, then increase the required count. Print this count at the end.

**Efficient Approach:** The idea is based on the fact that the multiples (1 to 9) of 1, 11, 111, etc has all its digits same.

For example:

1 times 1 = 1 (All digits are same) 2 times 1 = 2 (All digits are same) 3 times 1 = 3 (All digits are same) . . 9 times 1 = 9 (All digits are same) Similarly 1 times 11 = 11 (All digits are same) 2 times 11 = 22 (All digits are same) 3 times 11 = 33 (All digits are same) . . 9 times 11 = 99 (All digits are same) Same is the case for 111, 1111, etc.

Therefore, the steps can be defined as:

- Find the number of digits in R. This will decide the length of consecutive 1s to be created, till which we have to check.

For example, if R = 100, then length(R) = 3. Therefore we need to check only the multiples of 1, 11, and 111. - For each length of consecutive 1s from 1 to length(R):
- Multiply them with all values from
**2**to**9** - Check if it lies within the range
**[L, R]**or not. - If yes, then increment the count of required numbers.

- Multiply them with all values from
- Print the required count of numbers.

Below code is the implementation of the above approach:

## C++

`// C++ program to count the ` `// total numbers in the range ` `// L and R which have all the ` `// digit same ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that count the ` `// total numbersProgram between L ` `// and R which have all the ` `// digit same ` `int` `count_same_digit(` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `tmp = 0, ans = 0; ` ` ` ` ` `// length of R ` ` ` `int` `n = ` `log10` `(R) + 1; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// tmp has all digits as 1 ` ` ` `tmp = tmp * 10 + 1; ` ` ` ` ` `// For each multiple ` ` ` `// of tmp in range 1 to 9, ` ` ` `// check if it present ` ` ` `// in range [L, R] ` ` ` `for` `(` `int` `j = 1; j <= 9; j++) { ` ` ` ` ` `if` `(L <= (tmp * j) ` ` ` `&& (tmp * j) <= R) { ` ` ` ` ` `// Increment the required count ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `L = 12, R = 68; ` ` ` ` ` `cout << count_same_digit(L, R) ` ` ` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the ` `// total numbers in the range ` `// L and R which have all the ` `// digit same ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function that count the total ` `// numbersProgram between L and ` `// R which have all the digit same ` `static` `int` `count_same_digit(` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `tmp = ` `0` `, ans = ` `0` `; ` ` ` ` ` `// Length of R ` ` ` `int` `n = (` `int` `)Math.log10(R) + ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// tmp has all digits as 1 ` ` ` `tmp = tmp * ` `10` `+ ` `1` `; ` ` ` ` ` `// For each multiple of tmp ` ` ` `// in range 1 to 9, check if ` ` ` `// it present in range [L, R] ` ` ` `for` `(` `int` `j = ` `1` `; j <= ` `9` `; j++) ` ` ` `{ ` ` ` `if` `(L <= (tmp * j) && (tmp * j) <= R) ` ` ` `{ ` ` ` `// Increment the required count ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `L = ` `12` `, R = ` `68` `; ` ` ` ` ` `System.out.println(count_same_digit(L, R)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the ` `# total numbers in the range ` `# L and R which have all the ` `# digit same ` `import` `math ` ` ` `# Function that count the ` `# total numbersProgram between L ` `# and R which have all the ` `# digit same ` `def` `count_same_digit(L, R): ` ` ` ` ` `tmp ` `=` `0` `; ans ` `=` `0` `; ` ` ` ` ` `# length of R ` ` ` `n ` `=` `int` `(math.log10(R) ` `+` `1` `); ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` ` ` `# tmp has all digits as 1 ` ` ` `tmp ` `=` `tmp ` `*` `10` `+` `1` `; ` ` ` ` ` `# For each multiple ` ` ` `# of tmp in range 1 to 9, ` ` ` `# check if it present ` ` ` `# in range [L, R] ` ` ` `for` `j ` `in` `range` `(` `1` `, ` `9` `): ` ` ` ` ` `if` `(L <` `=` `(tmp ` `*` `j) ` `and` `(tmp ` `*` `j) <` `=` `R): ` ` ` ` ` `# Increment the required count ` ` ` `ans ` `+` `=` `1` `; ` ` ` ` ` `return` `ans; ` ` ` `# Driver Code ` `L ` `=` `12` `; R ` `=` `68` `; ` ` ` `print` `(count_same_digit(L, R)) ` ` ` `# This code is contributed by Nidhi_biet ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the ` `// total numbers in the range ` `// L and R which have all the ` `// digit same ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function that count the total ` `// numbersProgram between L and ` `// R which have all the digit same ` `static` `int` `count_same_digit(` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `tmp = 0, ans = 0; ` ` ` ` ` `// Length of R ` ` ` `int` `n = (` `int` `)Math.Log10(R) + 1; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// tmp has all digits as 1 ` ` ` `tmp = tmp * 10 + 1; ` ` ` ` ` `// For each multiple of tmp ` ` ` `// in range 1 to 9, check if ` ` ` `// it present in range [L, R] ` ` ` `for` `(` `int` `j = 1; j <= 9; j++) ` ` ` `{ ` ` ` `if` `(L <= (tmp * j) && (tmp * j) <= R) ` ` ` `{ ` ` ` `// Increment the required count ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `L = 12, R = 68; ` ` ` ` ` `Console.Write(count_same_digit(L, R)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity: **O(length(R))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.