Given a number N and a sum S, find the count of numbers upto N that have digit sum equal to S.
Examples: Input : N = 100, S = 4 Output : 5 Upto 100 only 5 numbers(4, 13, 22, 31, 40) can produce 4 as their sum of digits. Input : N = 1000, S = 1 Output : 4 Upto 1000 only 4 numbers(1, 10, 100 and 1000) can produce 1 as their sum of digits.
We can do a digit DP having state (current index, whether the currently constructed number of i digits is equal or less than the number formed by first i digits of N, the sum of digits of currently constructed number).
Let dp[i][tight][sum_so_far] denotes the count of numbers whose first i digits have been considered and tight denotes whether the currently constructed number is equal or less than number formed by first i digits of N. If tight is true, then it means that currently constructed number is equal to the number constructed by first i digits of N. If it is false then it means that currently constructed number is less than the number constructed by first i digits of N. sum_so_far denotes the sum of digits of currently constructed number.
If i = number of digits in N, sum_so_far = Sum, then answer = 1 else answer is 0.
For filling (i+1)th digit we can consider following –
If tight is true, then it means that our constructed number is still equal to number formed by first i digits of N. We can try our current possible digit value from 0 to (i+1)th digit of N. If we try the digit more than (i+1)th digit, then the constructed number will become greater than number formed by first i digits of N, which will violate the property that our constructed number should be <= N.
If tight is false, then it means that number constructed from first i – 1 digits has become less than number constructed from the first i – 1 digit of N, So it means that our number can never exceed N, so we can chose the any digit from 0 to 9.
nsum_so_far can be obtained by adding sum_so_far and current digit(currdigit).
Finally we will return answer which is count of numbers upto N that have digit sum equal to S.
Time Complexity: 2(tight) * Sum * log 10(N) * 10(transitions) = 20*log 10(N)*Sum.
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.
- Largest number less than N with digit sum greater than the digit sum of N
- Count of Binary Digit numbers smaller than N
- Count of N-digit numbers having digit XOR as single digit
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Largest number smaller than or equal to n and digits in non-decreasing order
- Count numbers with difference between number and its digit sum greater than specific value
- Count numbers less than N containing digits from the given set : Digit DP
- Check if frequency of each digit is less than the digit
- Count n digit numbers not having a particular digit
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Maximum sum subarray having sum less than or equal to given sum using Set
- Count of n digit numbers whose sum of digits equals to given sum
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Count numbers formed by given two digit with sum having given digits
- Numbers having difference with digit sum more than s
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of N digit Numbers having no pair of equal consecutive 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.