## Thursday, August 4, 2016

### Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 - GeeksforGeeks

Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 - GeeksforGeeks
Given number of digits n, print all n-digit numbers whose absolute difference between sum of digits at even and odd positions is 1. Solution should not consider leading 0's as digits.

The idea is to generate all n-digit numbers using recursion and maintain sum of even and odd digits so far in two separate variables. For a given position, we fill it with all digits from 0 to 9 and based on whether current position is even or odd, we increment even or odd sum. We handle leading 0’s case separately as they are not counted as digits.
We have followed Zero-based numbering like array indexes. i.e. leading(leftmost) digit is considered to be present in even position and digit next to it is considered at odd position and so on.
`// n --> value of input`
`// out --> output array`
`// index --> index of next digit to be filled in output array`
`// evenSum, oddSum --> sum of even and odd digits so far`
`void` `findNDigitNumsUtil(``int` `n, ``char``* out, ``int` `index, ``int` `evenSum,`
`                                                     ``int` `oddSum)`
`{`
`    ``// Base case`
`    ``if` `(index > n)`
`        ``return``;`
`    ``// If number becomes n-digit`
`    ``if` `(index == n)`
`    ``{`
`        ``// if absolute difference between sum of even and`
`        ``// odd digits is 1, print the number`
`        ``if` `(``abs``(evenSum - oddSum) == 1)`
`        ``{`
`            ``out[index] = ``'\0'``;`
`            ``cout << out << ``" "``;`
`        ``}`
`        ``return``;`
`    ``}`
`    ``// If current index is odd, then add it to odd sum and recurse`
`    ``if` `(index & 1)`
`    ``{`
`        ``for` `(``int` `i = 0; i <= 9; i++)`
`        ``{`
`            ``out[index] = i + ``'0'``;`
`            ``findNDigitNumsUtil(n, out, index + 1, evenSum, oddSum + i);`
`        ``}`
`    ``}`
`    ``else` `// else add to even sum and recurse`
`    ``{`
`        ``for` `(``int` `i = 0; i <= 9; i++)`
`        ``{`
`            ``out[index] = i + ``'0'``;`
`            ``findNDigitNumsUtil(n, out, index + 1, evenSum + i, oddSum);`
`        ``}`
`    ``}`
`}`
`// This is mainly a wrapper over findNDigitNumsUtil.`
`// It explicitly handles leading digit and calls`
`// findNDigitNumsUtil() for remaining indexes.`
`int` `findNDigitNums(``int` `n)`
`{`
`    ``// output array to store n-digit numbers`
`    ``char` `out[n + 1];`
`    ``// Initialize number index considered so far`
`    ``int` `index = 0;`
`    ``// Initialize even and odd sums`
`    ``int` `evenSum = 0, oddSum = 0;`
`    ``// Explicitly handle first digit and call recursive function`
`    ``// findNDigitNumsUtil for remaining indexes. Note that the`
`    ``// first digit is considered to be present in even position.`
`    ``for` `(``int` `i = 1; i <= 9; i++)`
`    ``{`
`        ``out[index] = i + ``'0'``;`
`        ``findNDigitNumsUtil(n, out, index + 1, evenSum + i, oddSum);`
`    ``}`
`}`

We can avoid using two variables evenSum and oddSum. Instead we can maintain single variable diff that stores difference between sum of even and odd digits. The implementation can be seen here.
// diff --> difference between sum of even and odd digits so far
void findNDigitNumsUtil(int n, char* out, int index, int diff)
{
// Base case
if(index > n)
return;

// If number becomes N-digit
if(index == n)
{
// if absolute difference between sum of even and
// odd digits is 1, print the number
if(abs(diff) == 1)
{
out[index] = '\0';
cout << out << " ";
}
return;
}

// If current index is odd, then add it to odd sum and recurse
if(index & 1)
for(int i = 0; i <= 9; i++)
{
out[index] = i + '0';
findNDigitNumsUtil(n, out, index + 1, diff - i);
}
else // else add to even sum and recurse
for(int i = 0; i <= 9; i++)
{
out[index] = i + '0';
findNDigitNumsUtil(n, out, index + 1, diff + i);
}
}

// This is mainly a wrapper over findNDigitNumsUtil.
// It explicitly handles leading digit and calls
// findNDigitNumsUtil() for remaining indexes.
int findNDigitNums(int n)
{
// output array to store N-digit numbers
char out[n + 1];

// Initialize number index considered so far
int index = 0;
// stoores diff between even and odd sums
int diff = 0;

// Explicitly handle first digit and call recursive function
// findNDigitNumsUtil for remaining indexes. Note that the
// first digit is considered to be present in even position.
for(int i = 1; i <= 9; i++)
{
out[index] = i + '0';
findNDigitNumsUtil(n, out, index + 1, diff + i);
}
}
Read full article from Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 - GeeksforGeeks