Given a number n, we need to count the 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 example, 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 depends on four variables: digits, esum (current even sum), osum (current odd sum), isEven(A flag to indicate whether the 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
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 integers in a range which have even number of odd digits and odd number of even digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of N-digit numbers having digit XOR as single digit
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Largest Even and Odd N-digit numbers in Octal Number System
- Largest Even and Odd N-digit numbers in Hexadecimal Number System
- Difference between sums of odd and even digits
- Smallest odd number with even sum of digits from the given number N
- Count of n digit numbers whose sum of digits equals to given sum
- Count numbers with difference between number and its digit sum greater than specific value
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized