# Count total number of N digit numbers such that the difference between sum of even and odd digits is 1

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.

Example

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.

## C++

`// A memoization based recursive program to count numbers ` `// with difference between odd and even digit sums as 1 ` `#include<bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// A lookup table used for memoization. ` `unsigned ` `long` `long` `int` `lookup[50][1000][1000][2]; ` ` ` `// Memnoization based recursive function to count numbers ` `// with even and odd digit sum difference as 1. This function ` `// conisders leading zero as a digit ` `unsigned ` `long` `long` `int` `countRec(` `int` `digits, ` `int` `esum, ` ` ` `int` `osum, ` `bool` `isOdd, ` `int` `n) ` `{ ` ` ` `// Base Case ` ` ` `if` `(digits == n) ` ` ` `return` `(esum - osum == 1); ` ` ` ` ` `// If current subproblem is already computed ` ` ` `if` `(lookup[digits][esum][osum][isOdd] != -1) ` ` ` `return` `lookup[digits][esum][osum][isOdd]; ` ` ` ` ` `// Initialize result ` ` ` `unsigned ` `long` `long` `int` `ans = 0; ` ` ` ` ` `// If current digit is odd, then add it to odd sum and recur ` ` ` `if` `(isOdd) ` ` ` `for` `(` `int` `i = 0; i <= 9; i++) ` ` ` `ans += countRec(digits+1, esum, osum+i, ` `false` `, n); ` ` ` `else` `// Add to even sum and recur ` ` ` `for` `(` `int` `i = 0; i <= 9; i++) ` ` ` `ans += countRec(digits+1, esum+i, osum, ` `true` `, n); ` ` ` ` ` `// Store current result in lookup table and return the same ` ` ` `return` `lookup[digits][esum][osum][isOdd] = ans; ` `} ` ` ` `// This is mainly a wrapper over countRec. It ` `// explicitly handles leading digit and calls ` `// countRec() for remaining digits. ` `unsigned ` `long` `long` `int` `finalCount(` `int` `n) ` `{ ` ` ` `// Initialize number digits considered so far ` ` ` `int` `digits = 0; ` ` ` ` ` `// Initialize all entries of lookup table ` ` ` `memset` `(lookup, -1, ` `sizeof` `lookup); ` ` ` ` ` `// Initializa final answer ` ` ` `unsigned ` `long` `long` `int` `ans = 0; ` ` ` ` ` `// Initialize even and odd sums ` ` ` `int` `esum = 0, osum = 0; ` ` ` ` ` `// Explicitly handle first digit and call recursive function ` ` ` `// countRec for remaining digits. Note that the first digit ` ` ` `// is considered as even digit. ` ` ` `for` `(` `int` `i = 1; i <= 9; i++) ` ` ` `ans += countRec(digits+1, esum + i, osum, ` `true` `, n); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `cout << ` `"Coutn of "` `<<n << ` `" digit numbers is "` `<< finalCount(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// A memoization based recursive ` `// program to count numbers with ` `// difference between odd and ` `// even digit sums as 1 ` `class` `GFG ` `{ ` ` ` `// A lookup table used for memoization. ` `static` `int` `[][][][]lookup = ` `new` `int` `[` `50` `][` `1000` `][` `1000` `][` `2` `]; ` ` ` `// Memnoization based recursive ` `// function to count numbers ` `// with even and odd digit sum ` `// difference as 1. This function ` `// conisders leading zero as a digit ` `static` `int` `countRec(` `int` `digits, ` `int` `esum, ` ` ` `int` `osum, ` `int` `isOdd, ` `int` `n) ` `{ ` ` ` `// Base Case ` ` ` `if` `(digits == n) ` ` ` `return` `(esum - osum == ` `1` `)?` `1` `:` `0` `; ` ` ` ` ` `// If current subproblem is already computed ` ` ` `if` `(lookup[digits][esum][osum][isOdd] != -` `1` `) ` ` ` `return` `lookup[digits][esum][osum][isOdd]; ` ` ` ` ` `// Initialize result ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// If current digit is odd, then ` ` ` `// add it to odd sum and recur ` ` ` `if` `(isOdd==` `1` `) ` ` ` `for` `(` `int` `i = ` `0` `; i <= ` `9` `; i++) ` ` ` `ans += countRec(digits+` `1` `, esum, osum+i, ` `0` `, n); ` ` ` `else` `// Add to even sum and recur ` ` ` `for` `(` `int` `i = ` `0` `; i <= ` `9` `; i++) ` ` ` `ans += countRec(digits+` `1` `, esum+i, osum, ` `1` `, n); ` ` ` ` ` `// Store current result in lookup ` ` ` `// table and return the same ` ` ` `return` `lookup[digits][esum][osum][isOdd] = ans; ` `} ` ` ` `// This is mainly a wrapper over countRec. It ` `// explicitly handles leading digit and calls ` `// countRec() for remaining digits. ` `static` `int` `finalCount(` `int` `n) ` `{ ` ` ` `// Initialize number digits considered so far ` ` ` `int` `digits = ` `0` `; ` ` ` ` ` `// Initialize all entries of lookup table ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `50` `; i++) ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `1000` `; j++) ` ` ` `for` `(` `int` `k = ` `0` `; k < ` `1000` `; k++) ` ` ` `for` `(` `int` `l = ` `0` `; l < ` `2` `; l++) ` ` ` `lookup[i][j][k][l] = -` `1` `; ` ` ` ` ` ` ` `// Initializa final answer ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Initialize even and odd sums ` ` ` `int` `esum = ` `0` `, osum = ` `0` `; ` ` ` ` ` `// Explicitly handle first digit and ` ` ` `// call recursive function countRec ` ` ` `// for remaining digits. Note that ` ` ` `// the first digit is considered ` ` ` `// as even digit. ` ` ` `for` `(` `int` `i = ` `1` `; i <= ` `9` `; i++) ` ` ` `ans += countRec(digits+` `1` `, esum + i, osum, ` `1` `, n); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver program ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.println(` `"Coutn of "` `+ n + ` ` ` `" digit numbers is "` `+ finalCount(n)); ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

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

## Recommended Posts:

- Count numbers with difference between number and its digit sum greater than specific value
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Count total number of digits from 1 to n
- Count of n digit numbers whose sum of digits equals to given sum
- Count numbers formed by given two digit with sum having given digits
- Total number of non-decreasing numbers with n digits
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count n digit numbers divisible by given number
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count Numbers with N digits which consists of even number of 0’s
- Count Numbers with N digits which consists of odd number of 0's
- Total numbers with no repeated digits in a range
- Count positive integers with 0 as a digit and maximum 'd' digits