Given a number n, we need to count total number of n digit numbers such that the sum of even digits is 1 more than the sum of odd digits. Here even and odd means positions of digits are like array indexes, for exampl, the leftmost (or leading) digit is considered as even digit, next to leftmost is considered as odd and so on.
Input: n = 2 Output: Required Count of 2 digit numbers is 9 Explanation : 10, 21, 32, 43, 54, 65, 76, 87, 98. Input: n = 3 Output: Required Count of 3 digit numbers is 54 Explanation: 100, 111, 122, ......, 980
We strongly recommend you to minimize your browser and try this yourself first.
This problem is mainly an extension of Count of n digit numbers whose sum of digits equals to given sum. Here the solution of subproblems depend on four variables: digits, esum (current even sum), osum (current odd sum), isEven(A flag to indicate whether current digit is even or odd).
Below is Memoization based solution for the same.
Count of 3 digit numbers is 54
Thanks to Gaurav Ahirwar for providing above solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Maximum profit from sale of wines
- Count number of subsets having a particular XOR value
- Count number of paths with at-most k turns
- Find the longest path in a matrix with given constraints
- Longest Repeating Subsequence
- Bitmasking and Dynamic Programming | Set 1 (Count ways to assign unique cap to every person)
- Maximum weight transformation of a given string
- Find all possible outcomes of a given expression
- Count of n digit numbers whose sum of digits equals to given sum
- Minimum Initial Points to Reach Destination
- Total number of non-decreasing numbers with n digits
- Find length of the longest consecutive path from a given starting character
- Compute sum of digits in all numbers from 1 to n
- Longest Even Length Substring such that Sum of First and Second Half is same
- Maximum Product Cutting | DP-36