Count numbers in a range with digit sum divisible by K having first and last digit different
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).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.