Given a range of values [L, R] and a value K, the task is to count the numbers in the given range which are divisible by at least K of the digits present in the decimal representation of that number.
Input: L = 24, R = 25, K = 2
24 has two digits 2 and 4 and is divisible by both 2 and 4. So this satisfies the given condition.
25 has two digits 2 and 5 and is only divisible by 5. But since K = 2, it doesnot qualifies the mentioned criteria.
Input: L = 5, R = 15, K = 1
Method 1: Naive Approach
- For any number between L to R, find the count of it’s digits that divides the number.
- If the count of number in the above step is greater than or equal to K, then include that number to the final count.
- Repeat the above steps for all numbers from L to R and print the final count.
Time Complexity: O(N), where N is the difference between the range [L, R].
Method 2: Efficient Approach
We will use the concept of Digit DP to solve this problem. Below are the observations to solve this problem:
- For all the positive integers(say a), to find the divisibility of the number from digits 2 to 9, the number a can be reduced as stated below to find the divisibility efficiently:
a = k*LCM(2, 3, 4, ..., 9) + q where k is integer and q lies between range [0, lcm(2, 3, ..9)] LCM(2, 3, 4, ..., 9) = 23x32x5x7 = 2520
- After performing a = a modulo 2520, we can find the count of digit from the original number a that divides this modulo.
Below are the steps to do so:
- Store all the digits of the given range and sort the digits in decreasing order.
- Traverse all the digits stored above and generate all the number which are strictly less than the given range of number.
- For generating the number less than the given number, use a variable tight such that:
- The value of tight is 0, denotes that by including that digit will give the number less than the given range.
- The value of tight is 1, denotes that by including that digit, it will give the number greater than the given range. So we can remove all permutations after getting tight value 1 to avoid more number of recursive calls.
- After generating all of the permutations of numbers, Find the number for which the count of digits dividing that number is greater than or equals to K.
- Store the count for each permuted number in dp table to use the result for Overlapping Subproblems.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count integers in the range [A, B] that are not divisible by C and D
- Count integers in a range which are divisible by their euler totient value
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check if the sum of digits of number is divisible by all of its digits
- Number of substrings divisible by 6 in a string of integers
- Number of substrings divisible by 4 in a string of integers
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Number of n-digits non-decreasing integers
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find N digits number which is divisible by D
- Smallest number with sum of digits as N and divisible by 10^N
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Number of digits to be removed to make a number divisible by 3
- Find a N-digit number such that it is not divisible by any of its digits
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 : SURENDRA_GANGWAR