![]() |
VOOZH | about |
Given three positive integers L, R and K, the task is to count the numbers in the range [L, R] whose product of digits is equal to K
Examples:
Input: L = 1, R = 130, K = 14
Output: 3
Explanation:
Numbers in the range [1, 100] whose sum of digits is K(= 14) are:
27 => 2 * 7 = 14
72 => 7 * 2 = 14
127 => 1 * 2 * 7 = 14
Therefore, the required output is 3.Input: L = 20, R = 10000, K = 14
Output: 20
Naive Approach: The simplest approach to solve this problem is to iterate over all the numbers in the range [L, R] and for every number, check if its product of digits is equal to K or not. If found to be true, then increment the count. Finally, print the count obtained.
Below is the implementation of the above approach:
20
Time Complexity: O(R - L + 1) * log10(R)
Auxiliary Space: O(1)
Efficient approach: To optimize the above approach, the idea is to use Digit DP. Following are the dynamic programming states of the Digit DP:
Dynamic programming states are:
prod: Represents sum of digits.
tight: Check if sum of digits exceed K or not.
end: Stores the maximum possible value of ith digit of a number.
st: Check if a number contains leading 0 or not.
Following are the Recurrence Relation of the Dynamic programming states:
cntNum(N, K, st, tight): Returns the count of numbers in the range [0, X] whose product of digits is K.
cntNum(N, K, st, tight): Returns the count of numbers in the range [0, X] whose product of digits is K.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
20
Time Complexity: O(K * log10(R) * 10 * 4)
Auxiliary Space: O(K * log10(R) * 4)
This approach takes three inputs, L, R, and K, and returns the count of numbers between L and R inclusive whose product of digits is equal to K. It does so by iterating over all numbers between L and R inclusive and checking if the product of their digits is equal to K using the reduce function from the functools module.
1. Initialize a counter variable to 0.
2. Iterate over all numbers between L and R inclusive using the range function.
3. For each number, convert it to a string and then apply the reduce function to compute the product of its digits.
4. Check if the product of digits is equal to K. If it is, increment the counter variable.
5. Return the counter variable as the output.
3 20
Time Complexity: O((R-L)*d), where d is the number of digits in R. This is because the code iterates over all numbers between L and R inclusive, and for each number, it applies the reduce function to compute the product of its digits, which takes O(d) time.
Auxiliary Space: O(d), where d is the number of digits in R. This is because the reduce function creates a new list of digits for each number and stores it in memory while computing the product of digits. The sum function also creates a new generator object, but it doesn't require any additional memory to store the result. Overall, the memory used by the code scales linearly with the number of digits in R.