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.
- 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>usingnamespacestd;// Function that count the// total numbersProgram between L// and R which have all the// digit sameintcount_same_digit(intL,intR){inttmp = 0, ans = 0;// length of Rintn =log10(R) + 1;for(inti = 0; i < n; i++) {// tmp has all digits as 1tmp = tmp * 10 + 1;// For each multiple// of tmp in range 1 to 9,// check if it present// in range [L, R]for(intj = 1; j <= 9; j++) {if(L <= (tmp * j)&& (tmp * j) <= R) {// Increment the required countans++;}}}returnans;}// Driver Programintmain(){intL = 12, R = 68;cout << count_same_digit(L, R)<< endl;return0;}Java
// Java program to count the// total numbers in the range// L and R which have all the// digit sameimportjava.util.*;classGFG{// Function that count the total// numbersProgram between L and// R which have all the digit samestaticintcount_same_digit(intL,intR){inttmp =0, ans =0;// Length of Rintn = (int)Math.log10(R) +1;for(inti =0; i < n; i++){// tmp has all digits as 1tmp = tmp *10+1;// For each multiple of tmp// in range 1 to 9, check if// it present in range [L, R]for(intj =1; j <=9; j++){if(L <= (tmp * j) && (tmp * j) <= R){// Increment the required countans++;}}}returnans;}// Driver codepublicstaticvoidmain(String[] args){intL =12, R =68;System.out.println(count_same_digit(L, R));}}// This code is contributed by offbeatPython3
# Python3 program to count the# total numbers in the range# L and R which have all the# digit sameimportmath# Function that count the# total numbersProgram between L# and R which have all the# digit samedefcount_same_digit(L, R):tmp=0; ans=0;# length of Rn=int(math.log10(R)+1);foriinrange(0, n):# tmp has all digits as 1tmp=tmp*10+1;# For each multiple# of tmp in range 1 to 9,# check if it present# in range [L, R]forjinrange(1,9):if(L <=(tmp*j)and(tmp*j) <=R):# Increment the required countans+=1;returnans;# Driver CodeL=12; R=68;print(count_same_digit(L, R))# This code is contributed by Nidhi_bietC#
// C# program to count the// total numbers in the range// L and R which have all the// digit sameusingSystem;classGFG{// Function that count the total// numbersProgram between L and// R which have all the digit samestaticintcount_same_digit(intL,intR){inttmp = 0, ans = 0;// Length of Rintn = (int)Math.Log10(R) + 1;for(inti = 0; i < n; i++){// tmp has all digits as 1tmp = tmp * 10 + 1;// For each multiple of tmp// in range 1 to 9, check if// it present in range [L, R]for(intj = 1; j <= 9; j++){if(L <= (tmp * j) && (tmp * j) <= R){// Increment the required countans++;}}}returnans;}// Driver codepublicstaticvoidMain(){intL = 12, R = 68;Console.Write(count_same_digit(L, R));}}// This code is contributed by Code_MechOutput:5
Time Complexity: O(length(R))
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.


