Given a range represented by two positive integers L and R and a positive integer K. Find the count of numbers in the range where the number does not contain more than K non zero digits.
Input : L = 1, R = 1000, K = 3 Output : 1000 Explanation : All the numbers from 1 to 1000 are 3 digit numbers which obviously cannot contain more than 3 non zero digits. Input : L = 9995, R = 10005 Output : 6 Explanation : Required numbers are 10000, 10001, 10002, 10003, 10004 and 10005
Prerequisites : Digit DP
There can be two approaches to solve this type of problem, one can be a combinatorial solution and other can be a dynamic programming based solution. Below is a detailed approach of solving this problem using a digit dynamic programming approach.
Dynamic Programming Solution : Firstly, if we are able to count the required numbers upto R i.e. in the range [0, R], we can easily reach our answer in the range [L, R] by solving for from zero to R and then subtracting the answer we get after solving for from zero to L – 1. Now, we need to define the DP states.
- Since we can consider our number as a sequence of digits, one state is the position at which we are currently in. This position can have values from 0 to 18 if we are dealing with the numbers upto 1018. In each recursive call, we try to build the sequence from left to right by placing a digit from 0 to 9.
- Second state is the count which defines the number of non zero digits, we have placed in the number we are trying to build.
- Another state is the boolean variable tight which tells the number we are trying to build has already become smaller than R, so that in the upcoming recursive calls we can place any digit from 0 to 9. If the number has not become smaller, maximum limit of digit we can place is digit at current position in R.
In the final recursive call, when we are at the last position if the count of non zero digits is less than or equal to K, return 1 otherwise return 0.
Below is the implementation of the above approach.
Time Complexity : O(18 * 18 * 2 * 10), if we are dealing with the numbers upto 1018
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find two numbers whose sum is N and does not contain any digit as K
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Check if a String contains Anagrams of length K which does not contain the character X
- DFA that begins with 'a' but does not contain substring 'aab'
- Length of the longest increasing subsequence which does not contain a given sequence as Subarray
- Divide array into two arrays which does not contain any pair with sum K
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Find the numbers from 1 to N that contains exactly k non-zero digits
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- How to store a very large number of more than 100 digits in C++
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count number of rotated strings which have more number of vowels in the first half than second half
- Smallest multiple of 3 which consists of three given non-zero digits
- Check if the Xor of the frequency of all digits of a number N is zero or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.