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 others can be a dynamic programming based solution. Below is a detailed approach to solving this problem using digit dynamic programming.
Dynamic Programming Solution: Firstly, if we are able to count the required numbers up to 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 up to 1018. In each recursive call, we try to build the sequence from left to right by placing a digit from 0 to 9.
- The 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.
- The 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, the maximum limit of digit we can place is digit at the current position in R.
In each recursive call, we set the last digit as the digit we placed in the last position, and we set the 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
We can also solve this problem by recursion, for every position we can fill any number which satisfies the given condition except for the first and last position because they will be paired together, and for this, we will use recursion and in every call just check if the first number is smaller or larger than the last number if it turns out to be greater than we will add 8 otherwise 9 and call for number / 10, once the number becomes smaller than 10 first and the last digit becomes same so return the number itself.
Below is the implementation of the above approach
- 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
- Count of N-digit numbers having digit XOR as single digit
- Last digit of sum of numbers in the given range in the Fibonacci series
- 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
- Find Last Digit of a^b for Large Numbers
- Last digit of Product of two Large or Small numbers (a * b)
- Count n digit numbers not having a particular digit
- 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
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.
Improved By : sanjeev2552, raj143125, 29AjayKumar, reapedjuggler, divyeshrabadiya07, more