Given a range represented by two positive integers L and R. Find the count of numbers in the range where the first digit is equal to the last digit of the number.
Input : L = 2, R = 60 Output : 13 Explanation : Required numbers are 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44 and 55 Input : L = 1, R = 1000 Output : 108
Prerequisites : Digit DP
There can be two approaches to solve this type of problem, one can be a combinatorial solution and other can be a dynamic programming based solution. Below is a detailed approach of solving this problem using a digit dynamic programming.
Dynamic Programming Solution : Firstly, if we are able to count the required numbers upto R i.e. in the range [0, R], we can easily reach our answer in the range [L, R] by solving for from zero to R and then subtracting the answer we get after solving for from zero to L – 1. Now, we need to define the DP states.
- Since we can consider our number as a sequence of digits, one state is the position at which we are currently in. This position can have values from 0 to 18 if we are dealing with the numbers upto 1018. In each recursive call, we try to build the sequence from left to right by placing a digit from 0 to 9.
- Second state is the firstD which defines the first digit of the number we are trying to build and can have values from 0 to 9.
- Third state is the lastD which defines the last digit of the number we are trying to build and can have values from 0 to 9.
- Another state is the boolean variable tight which tells the number we are trying to build has already become smaller than R so that in the upcoming recursive calls we can place any digit from 0 to 9. If the number has not become smaller, maximum limit of digit we can place is digit at current position in R.
In each recursive call, we set last digit as the digit we placed in the last position and we set first digit as the first non zero digit of the number. In the final recursive call, when we are at the last position if the first digit is equal to the last digit, return 1, otherwise 0.
Below is the implementation of the above approach.
Time Complexity : O(18 * 10 * 10 * 2 * 10), if we are dealing with the numbers upto 1018
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Count numbers with unit digit k in given range
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count numbers (smaller than or equal to N) with given digit sum
- Count of Numbers in a Range where digit d occurs exactly K times
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count n digit numbers not having a particular digit
- Count n digit numbers divisible by given number
- Count numbers with difference between number and its digit sum greater than specific value
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of integers from the range [0, N] whose digit sum is a multiple of K
- Count 'd' digit positive integers with 0 as a digit
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Find the highest occurring digit in prime numbers in a range
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.