Prerequisites: Dynamic Programming, DigitDP
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find if it is possible to choose subarray that it contains exactly K even integers
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Count of subarrays which contains a given number exactly K times
- Sum of nodes of Linked List whose contains values with exactly three factors
- Minimum digits to be removed to make either all digits or alternating digits same
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Numbers less than N which are product of exactly two distinct prime numbers
- Smallest multiple of N with exactly N digits in its Binary number representation
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers from given range having odd digits at odd places and even digits at even places
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Check if given number contains a digit which is the average of all other digits
- Check if N contains all digits as K in base B
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Find the Largest number with given number of digits and sum 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 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.