Given a number N, the task is to find the sum of all N-digit palindromic numbers which are divisible by 9 and the number doesn’t contain 0 in it.
Note: The sum can be very large, take modulo with 109+7.
Input: N = 2
There is only one 2-digit palindromic number divisible by 9 is 99.
Input: N = 3
Naive Approach: The idea is to iterate over all the N-digit numbers and check whether the numbers are palindromic and divisible by 9 and doesn’t contain zero in it. If yes then the summation of all the numbers given the required result.
Efficient Approach: Below are the some observation for this problem statement:
- If N is odd, then the number can be of the form “ab..x..ba” and if N is even then the number can be “ab..xx..ba”, where ‘a’, ‘b’ and ‘x’ can be replaced by digit from 1 to 9.
- If number “ab..x..ba” divisible by 9, (2*(a+b) + x) must be divisible by 9.
- For every possible pair of (a, b) there always exist one such ‘x’ so that the number is divisible by 9.
- Then the count of N-digit palindromic numbers which are divisible by 9 can be calculated from below formula:
Since we have 9 digits to fill at the position a and b. Therefore, total possible combinations will be: if N is Odd then, count = pow(9, N/2) if N is Even then, count = pow(9, N/2 - 1) as N/2 digits are repeated at the end to get the number palindromic.
Proof of Uniqueness of ‘x’:
- Minimum value of a and b is 1, therefore minimum sum = 2.
- Maximum value of a and b is 9, therefore minimum sum = 18.
- As the numbers are palindromic, then the sum is given by:
sum = 2*(a+b) + x;
- 2*(a+b) will be of the form 2, 4, 6, 8, …, 36. To make sum divisible by 9 the value of x can be:
sum one of the possible value of x sum + x 2 7 9 4 5 9 6 3 9 8 1 9 . . . . . . . . . 36 9 45
Below are the steps:
- After the above observations, the sum of the digit of all N-digit palindromic number which is divisible by 9 at any kth position is given by:
sum at kth position = 45*(number of combinations)/9
- Iterate a loop over [1, N] and find the number of possible combination to placed a digit at current index.
- Find the sum of all the digits at current digit using the above mentioned formula.
- The summation of all the sum calculated at each iteration given the required result.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the given number.
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Sum and Product of all Composite numbers which are divisible by k in an array
- Count numbers which are divisible by all the numbers from 2 to 10
- Sum of all numbers in the given range which are divisible by M
- Count of N digit palindromic numbers divisible by 9
- Sum of first N natural numbers which are divisible by 2 and 7
- Find permutation of n which is divisible by 3 but not divisible by 6
- Sum of all odd length palindromic numbers within the range [L, R]
- Sum of all palindromic numbers lying in the range [L, R] for Q queries
- Check if a string contains a palindromic sub-string of even length
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Sum of numbers from 1 to N which are divisible by 3 or 4
- Sum of first N natural numbers which are divisible by X or Y
- Split a number as sum of K numbers which are not divisible by K
- Sum of first K numbers which are not divisible by N
- Check if any permutation of a number is divisible by 3 and is Palindromic
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Check if given number contains a digit which is the average of all other digits
- Length of the smallest substring which contains all vowels