Given a range in the form of L and R, and a value K, the task is to count the numbers between the range L to R such that the sum of the digit is divisible by K, and the first digit is not equal to the last digit of the number.
Input: L = 1, R = 10, K = 2
The input numbers whose sum of the digits are divisible by 2 (K = 2) are 2, 4, 6, 8 but the first digit of all these numbers is equal to the last digit. Therefore the answer is 0, which means none of the digit satisfies the condition.
Input: L = 10, R = 20, K = 2
The numbers whose sum of digits are divisible by 2 (K = 2) are 11, 13, 15, 17, 19, and 20. But in 11 the first digit matches with the last digit. So excluding this, the answer will be 5.
The naive approach for solving this problem is to check every number whether it satisfies the given condition or not. However, this solution will work inefficiently because of the range of numbers.
The main idea for solving this problem is to use Digit Dynamic Programming.
There are various states that need to be defined for the transition. So, we can define the DP states as:
- Since in digit DP, we think a number as a sequence of digits so for traversing the sequence, we need position as a state. What we generally do, is place every possible digit [0, 9] in the recursive call, which we can put at every position to find possible solution provided all conditions should be satisfied.
- The second thing which we need is the sum, upto which we have built our sequence. Since the sum can be large, so we can keep sum as sum modulo K for better space and time complexity. So the sum is the second DP state.
- The third thing which we need is the digit, which we have placed in the first position. While placing the last digit of the sequence, we need to check whether it is equal to the first digit, which we have placed earlier or not. So, this is going to be the third DP state.
- The problem is at each time we create an N-digit sequence, where N is the number of digits present in the upper bound of the number, it doesn’t create 1 if (N=3), but it creates 001. Here the first digit is 0, and the last digit is 1, but this is wrong as we have created a single-digit number whose first digit is equal to the last digit. So, we have to check this every single time in the recursive call. Let suppose we have to build a sequence like 0002 where we need to update our starting digit equal to 2. So a prefix of zeros should be neglected. To check this, we need a new boolean state (0 or 1). This is the fourth DP state.
- The last thing which we need is to check whether the number which we are building does not exceed the upper bound. Let suppose we are building a number less than equal to 456. We have created a sequence like 45, so at the 3rd place, we can’t put any digit between 0 to 9. Here we can place only numbers from 0 to 6. So for checking this bound, we need an extra boolean state. This is our last DP state.
- At every position, we calculate the sum of digits and remove the prefix of zeros.
- The first non-zero digit will be our starting digit of the sequence.
- In the last position, we will check whether it is matching with the starting digit or not.
Below is the implementation of the above approach.
Time Complexity: O(18*1000*10*2*2), which is nearly O(2*10^6).
- 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 of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of Numbers in a Range divisible by m and having digit d in even positions
- N digit numbers having difference between the first and last digits as K
- Count non-palindromic array elements having same first and last digit
- Count of N-digit numbers having digit XOR as single digit
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Last digit of a number raised to last digit of N factorial
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Count all numbers up to N having M as the last digit
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Last digit of sum of numbers in the given range in the Fibonacci series
- Find the remainder when First digit of a number is divided by its Last digit
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count n digit numbers not having a particular digit
- Sum of last digit of all integers from 1 to N divisible by M
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 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 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.