Given a number n and a digit d, count all occurrences of d in range from 0 to n.
Input : n = 25 d = 2 Output : 9 The occurrences are 2, 12, 20, 21 22 (Two occurrences), 23, 24, 25 Input : n = 25 d = 3 Output :3 The occurrences are 3, 13, 23 Input : n = 32 d = 3 Output : 6 The occurrences are 3, 13, 23, 30, 31, 32
The first occurrence of d cannot be before number d. So we start to iterate from d and do following check again and again. We jump the number by 10 most of the time (in step 2) except for the cases mentioned in steps 2.a and 2.b.
Step 1: Check whether the last digit of the number is equal to the d, if it is then increment the count.
a) If the number is completely divisible by 10 then increment both the count and number (For example if we reach the number 30 which is completely divisible by 10 and d=3, then we have to count all numbers from 31-39 that’s why we increment count by 1 and number by 1)
b) else if the first digit of the number is one less than d then it means that we have come to the row where we have to increment the number by 10 and subtract the d from it. For example if we reach 23 for d=3 then we increment the number by 23+10-3 = 30)
c) else increment the number by 10 only. (For example: d=3, itr=3, then increment by 10 i.e.13, 23)
Step 3:- Return the count.
This article is contributed by Rakesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find the highest occurring digit in prime numbers in a range
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Number of occurrences of 2 as a digit in numbers from 0 to n
- Find the remainder when First digit of a number is divided by its Last digit
- Count the number of occurrences of a particular digit in a number
- Biggest integer which has maximum digit sum in range from 1 to n
- Last digit of sum of numbers in the given range in the Fibonacci series
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count numbers with unit digit k in given range
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of numbers in range which are divisible by M and have digit D at odd places