Given an Integer N, and a number K, the task is to find out the total numbers from 0 to N which have exactly K non zero digits and the sum of those digits should be odd and that sum should be distinct. The number N can be as large as 10^18.
Input : N = 10, K = 1
Output : 5
The numbers which follow the conditions are ->
1, 3, 5, 7 and 9
The digit sum of 10 that is (1+0) = 1 is also odd, but 1 is already included in our count.
Input : N = 100, K = 2
Output : 8
A naive approach for linear traversing in O(N) of all elements from 0 to N and calculating sum of digits in log(n) where n is the number of digits in that number will fail for large inputs of N.
- We can use Dynamic Programming and it’s very useful technique that is digit-dp to solve this problem.
- So instead of keeping a record of non-zero integers, we keep a record of zeroes we can keep at different indices, idx in variable K. The number of zeroes we can keep can be found initially by subtracting K with the number of digits in N.
- We keep all the digits of N into a vector say, digits.
Now, we calculate range of elements we can keep at index idx by analysing K.
- Suppose at index idx, we are left with K = 1 (A Non-zero value), then our range to put elements is [0, j] where j is the upper bound decided by the tight value obtained from the current index of the digit from vector digits.
- If at idx, we are left with K = 0, then our range becomes [1, j] because we can’t put in 0 there.
- Now, also take a parameter that is sum, which will calculate the sum of digits of a number till the base case hits successfully.
- Also, a boolean map is used which will store all the odd sums calculated already, so it gives distinct odd sums.
The recurrence will be:
where j = digits[idx] if tight = 0, else j = 9
When idx = digits.size(), K == 0 and sum is odd.
We mark the sum as true and return 1 else return 0.
- If idx > digits.size() then return 0.
- When idx = digits.size(), K == 0 and sum is odd.
So we create a DP table say DP[idx][K][tight][sum] which will store our result from the recurrence above and return the count by memoizing it to this DP table.
Below is the implementation of the above approach:
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.
- Count of N-digit numbers with all distinct digits
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Count numbers less than N containing digits from the given set : Digit DP
- Count numbers formed by given two digit with sum having given digits
- Count of n digit numbers whose sum of digits equals to given sum
- 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 total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of Numbers in Range where first digit is equal to last digit of the number
- Check if all sub-numbers have distinct Digit product
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Numbers having Unique (or Distinct) digits
- Count distinct pairs from two arrays having same sum of digits
- N digit numbers divisible by 5 formed from the M digits
- Print all n-digit numbers whose sum of digits equals to given sum
- 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 upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
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.