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
- 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
- Last digit of a number raised to last digit of N factorial
- Find the remainder when First digit of a number is divided by its Last digit
- N digit numbers having difference between the first and last digits as K
- Last digit of sum of numbers in the given range in the Fibonacci series
- Count of N-digit numbers having digit XOR as single digit
- Count non-palindromic array elements having same first and last digit
- Check if the first and last digit of the smallest number forms a prime
- Count numbers with same first and last digits
- Count all numbers up to N having M as the last digit
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Count numbers with unit digit k in given range
- 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 of numbers from the range [L, R] which contains at least one digit that divides K
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Count of numbers in range which are divisible by M and have digit D at odd places
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.