


Input: ‘L’ = 1, ‘R’ = 12, ‘D’ = 2, ‘K’ = 1
Output: 2
As in the range, numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, and 2, 12 are the numbers where the digit ‘2’ comes exactly once.
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains ‘L’ and ‘R’ the left and right boundaries of the range.
The second line contains ‘D’ the digit for which we have to check.
The third line contains 'K' the count of the digit 'D'.
For each test case, print the count of numbers satisfying the given criteria.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= L <= R <= 10^9
0 <= D <= 9
0 <= K <= 18
Time Limit: 1 sec
In the naive approach, we will loop from ‘L to ‘R’ and for each number ‘I’ we will check if the count of ‘D’ in number ‘I’ is equal to the number ‘K’ then increase the answer and return that answer.
In this approach, we will find solutions for ‘R’ and ‘L’-1 and then subtract the solution of ‘L’-1 from the solution of ‘R’ to get the answer for the range [L, R]. For each ‘num’ number from 1 to ‘X’ where ‘X’ will either be ‘R’ or ‘L’ - 1 we have to check if the count of the number ‘d’ in ‘num’ is equal to ‘k’ or not if it is ‘k’ then we have to increase our answer by 1.
e.g. for ‘X’ = 5, ‘d’ = 1, and ‘k’ = 1, We can see there is only one number in the range 1 to 5 is there where the count of digit 1 is equal to 1 and the number is 1 itself, But this process will give TLE as the value of ‘X’ is up to 10^9.
So we will apply dynamic programming to it. We will check for each digit in the number what are the possibilities of number we can put at each digit so for a value of ‘X’ which is equal to 5000(assume). We have 4 places of digits ( _ _ _ _ ) where we have to check/put digits such that the resulting digit is less than or equal to 5000 also the requirement for the ‘d’ and ‘k’ is satisfied,
So initially, we have the range of 0 to 9 which we can put at each position but when we put a digit which makes our number greater than the range for example 5000 if we put 6 at first place and all the remaining digits equal to 000 then our number will be 6000>5000 so for checking what is the maximum digit we can put at the current place we will create a flag which will tell us in previous digit locations if we had put a digit which makes the number equal to respective range number for example if we put 5 at the first place of of number then later digits should be 0 else the number will get greater than 5000 so the range of the number is [0, 9] if the flag is high else set the value of limit to [0, current digit at the number ‘X’] after each digit value we have to set the value of flag to true if the current digit is the less then respective digit in ‘X’ else set it to false.
Also, we have to check the count of the digit ‘d’ in the number we are making because we have completed or put all the digits at all the positions then we will check the count of ‘d’ if it is equal to ‘k’ then increase answer by 1 now we have one special case if ‘d’ = 0 then we can not count leading zeros like 0004 into the count so for that we will put a ‘zFlag’ to check if there are no leading zeros to be counted.
We will create a 4D- array named DP which will store the position of the digit, count of the digit ‘d’, flag and zflag.