Given two integers N and K. The task is to find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.
Input: N = 100, K = 1
The digits with exactly 1 non zero digits between 1 and 100 are:
1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 20, 30, 40, 50, 60, 70, 80, 90, 100
Input: N = 25, K = 2
The digits with exactly 2 non zero digits between 1 and 25 are:
11, 12, 13, 14, 15, 16, 17,
18, 19, 21, 22, 23, 24, 25
Approach: It is enough to consider the integers of N digits, by filling the higher digits with 0 if necessary. This problem can be solved by applying the method called digit DP.
- dp[i][j] = The higher i digits have already been decided, and there are j non-zero digits, and it has already been determined that it is less than N.
- dp[i][j] = The higher i digits have already been decided, and there are j non-zero digits, and it has not yet been determined that it is less than N.
After computing the above dp, the desired answer is dp[L][K] + dp[L][K], where L is the number of digits of N.
Below is the implementation of the above approach:
Time Complexity: O(LK) where L is the number of digits in N.
Note: The two for loops used to calculate the state which from [0, 1] and [0, 9] respectively are considered as a constant multiplication.
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.
- Find all palindrome numbers of given digits
- Find all the prime numbers of given number of digits
- Find all Autobiographical Numbers with given number of digits
- Find the total count of numbers up to N digits in a given base B
- Find all the possible numbers in a range that can be evenly divided by its digits
- Find maximum product of digits among numbers less than or equal to N
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find the average of k digits from the beginning and l digits from the end of the given number
- Find smallest number with given number of digits and sum of digits under given constraints
- Compute sum of digits in all numbers from 1 to n
- Count numbers with same first and last digits
- Sum of the products of same placed digits of two numbers
- Count numbers less than N containing digits from the given set : Digit DP
- Sort the numbers according to their product of 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.