Given a positive integer N, the task is to count the number of N-digit numbers where each digit in the number is the mean of its two adjacent digits.
Examples :
Input: N = 1
Output: 10
Explanation: All numbers from 0 to 9 satisfy the given condition as there is only one digit.Input: N = 2
Output: 90
Naive Approach: The simplest approach to solve the given problem is to iterate over all possible N-digit numbers and count such numbers where each digit is the mean of the two adjacent digits. After checking for all the numbers, print the value of count as the result.
Time Complexity: O(N × 10N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using Dynamic Programming because the above problem has Overlapping subproblems and an Optimal substructure. The subproblems can be stored in dp[][][] table using memoization where dp[digit][prev1][prev2] stores the answer from the digitth position till the end, when the previous digit selected, is prev1 and the second previous digit selected is prev2. Follow the steps below to solve the problem:
- Define a recursive function, say countOfNumbers(digit, prev1, prev2) by performing the following steps.
- If the value of digit is equal to N + 1 then return 1 as a valid N-digit number is formed.
- If the result of the state dp[digit][prev1][prev2] is already computed, return this state dp[digit][prev1][prev2].
- If the current digit is 1, then any digit from [1, 9] can be placed. If N = 1, then 0 can be placed as well.
- If the current digit is 2, then any digit from [0, 9] can be placed.
- Otherwise, consider the three numbers prev1, prev2, and the current digit to be placed which is not yet decided. Among these three numbers, prev1 must be the mean of prev2 and current digit. Hence, current digit = (2*prev1) – prev2. If current >= 0 and current <= 9 then we can place it in the given position. Else return 0.
- Since mean involves integer division, (current + 1) can also be placed at the current position if (current + 1) >= 0 and (current + 1) ? 9.
- After making a valid placement, recursively call the countOfNumbers function for index (digit + 1).
- Return the sum of all possible valid placements of digits as the answer.
- Print the value returned by the function countOfNumbers(1, 0, 0, N) as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; int dp[100][10][10]; // Function to find number of 'N' // digit numbers such that the element // is mean of sum of its adjacent digits int countOfNumbers( int digit, int prev1, int prev2, int n) { // If digit = n + 1, a valid // n-digit number has been formed if (digit == n + 1) { return 1; } // If the state has // already been computed int & val = dp[digit][prev1][prev2]; if (val != -1) { return val; } val = 0; // If current position is 1, // then any digit from [1-9] // can be placed. // If n = 1, 0 can be also placed. if (digit == 1) { for ( int i = (n == 1 ? 0 : 1); i <= 9; ++i) { val += countOfNumbers( digit + 1, i, prev1, n); } } // If current position is 2, // then any digit from [1-9] // can be placed. else if (digit == 2) { for ( int i = 0; i <= 9; ++i) { val += countOfNumbers( digit + 1, i, prev1, n); } } else { // previous digit selected is the mean. int mean = prev1; // mean = (current + prev2) / 2 // current = (2 * mean) - prev2 int current = (2 * mean) - prev2; // Check if current and current+1 // can be valid placements if (current >= 0 and current <= 9) val += countOfNumbers(digit + 1, current, prev1, n); if ((current + 1) >= 0 and (current + 1) <= 9) val += countOfNumbers(digit + 1, current + 1, prev1, n); } // return answer return val; } // Driver code int main() { // Initializing dp array with -1. memset (dp, -1, sizeof dp); // Given Input int n = 2; // Function call cout << countOfNumbers(1, 0, 0, n) << endl; return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ static int dp[][][] = new int [ 100 ][ 10 ][ 10 ]; // Function to find number of 'N' // digit numbers such that the element // is mean of sum of its adjacent digits static int countOfNumbers( int digit, int prev1, int prev2, int n) { // If digit = n + 1, a valid // n-digit number has been formed if (digit == n + 1 ) { return 1 ; } // If the state has // already been computed int val = dp[digit][prev1][prev2]; if (val != - 1 ) { return val; } val = 0 ; // If current position is 1, // then any digit from [1-9] // can be placed. // If n = 1, 0 can be also placed. if (digit == 1 ) { for ( int i = (n == 1 ? 0 : 1 ); i <= 9 ; ++i) { val += countOfNumbers( digit + 1 , i, prev1, n); } } // If current position is 2, // then any digit from [1-9] // can be placed. else if (digit == 2 ) { for ( int i = 0 ; i <= 9 ; ++i) { val += countOfNumbers( digit + 1 , i, prev1, n); } } else { // previous digit selected is the mean. int mean = prev1; // mean = (current + prev2) / 2 // current = (2 * mean) - prev2 int current = ( 2 * mean) - prev2; // Check if current and current+1 // can be valid placements if (current >= 0 && current <= 9 ) val += countOfNumbers(digit + 1 , current, prev1, n); if ((current + 1 ) >= 0 && (current + 1 ) <= 9 ) val += countOfNumbers(digit + 1 , current + 1 , prev1, n); } // return answer return val; } // Driver code public static void main(String[] args) { // Initializing dp array with -1. for ( int i = 0 ; i < 100 ; i++) { for ( int j = 0 ; j < 10 ; j++) { for ( int k = 0 ; k < 10 ; k++) { dp[i][j][k] = - 1 ; } } } // Given Input int n = 2 ; // Function call System.out.println(countOfNumbers( 1 , 0 , 0 , n)); } } // This code is contributed by sanjoy_62. |
Python3
# python program for the above approach dp = [[[ - 1 for i in range ( 10 )] for col in range ( 20 )] for row in range ( 100 )] # Function to find number of 'N' # digit numbers such that the element # is mean of sum of its adjacent digits def countOfNumbers(digit, prev1, prev2, n): # If digit = n + 1, a valid # n-digit number has been formed if (digit = = n + 1 ): return 1 # If the state has # already been computed val = dp[digit][prev1][prev2] if (val ! = - 1 ): return val val = 0 # If current position is 1, # then any digit from [1-9] # can be placed. # If n = 1, 0 can be also placed. if (digit = = 1 ): start = 1 if (n = = 1 ): start = 0 for i in range (start, 10 ): val + = countOfNumbers(digit + 1 , i, prev1, n) # If current position is 2, # then any digit from [1-9] # can be placed. elif (digit = = 2 ): for i in range ( 0 , 10 ): val + = countOfNumbers(digit + 1 , i, prev1, n) else : # previous digit selected is the mean. mean = prev1 # // mean = (current + prev2) / 2 # // current = (2 * mean) - prev2 current = ( 2 * mean) - prev2 # Check if current and current+1 # can be valid placements if (current > = 0 and current < = 9 ): val + = countOfNumbers(digit + 1 , current, prev1, n) if ((current + 1 ) > = 0 and (current + 1 ) < = 9 ): val + = countOfNumbers(digit + 1 , current + 1 , prev1, n) # return answer return val # Driver code # Initializing dp array with -1. # Given Input n = 2 # Function call print (countOfNumbers( 1 , 0 , 0 , n)) #cThis code is contributed by amreshkumar3 |
C#
// C# program for the above approach using System; class GFG{ static int [,,] dp = new int [100, 10, 10]; // Function to find number of 'N' // digit numbers such that the element // is mean of sum of its adjacent digits static int countOfNumbers( int digit, int prev1, int prev2, int n) { // If digit = n + 1, a valid // n-digit number has been formed if (digit == n + 1) { return 1; } // If the state has // already been computed int val = dp[digit, prev1, prev2]; if (val != -1) { return val; } val = 0; // If current position is 1, // then any digit from [1-9] // can be placed. // If n = 1, 0 can be also placed. if (digit == 1) { for ( int i = (n == 1 ? 0 : 1); i <= 9; ++i) { val += countOfNumbers( digit + 1, i, prev1, n); } } // If current position is 2, // then any digit from [1-9] // can be placed. else if (digit == 2) { for ( int i = 0; i <= 9; ++i) { val += countOfNumbers( digit + 1, i, prev1, n); } } else { // previous digit selected is the mean. int mean = prev1; // mean = (current + prev2) / 2 // current = (2 * mean) - prev2 int current = (2 * mean) - prev2; // Check if current and current+1 // can be valid placements if (current >= 0 && current <= 9) val += countOfNumbers(digit + 1, current, prev1, n); if ((current + 1) >= 0 && (current + 1) <= 9) val += countOfNumbers(digit + 1, current + 1, prev1, n); } // return answer return val; } // Driver Code static public void Main () { // Initializing dp array with -1. for ( int i = 0; i < 100; i++) { for ( int j = 0; j < 10; j++) { for ( int k = 0; k < 10; k++) { dp[i, j, k] = -1; } } } // Given Input int n = 2; // Function call Console.Write(countOfNumbers(1, 0, 0, n)); } } // This code is contributed by code_hunt. |
Javascript
<script> // javascript program for the above approach var dp = Array(100).fill().map(() =>Array(10).fill().map(()=>Array(10).fill(-1))); // Function to find number of 'N' // digit numbers such that the element // is mean of sum of its adjacent digits function countOfNumbers(digit , prev1, prev2 , n) { // If digit = n + 1, a valid // n-digit number has been formed if (digit == n + 1) { return 1; } // If the state has // already been computed var val = dp[digit][prev1][prev2]; if (val != -1) { return val; } val = 0; // If current position is 1, // then any digit from [1-9] // can be placed. // If n = 1, 0 can be also placed. if (digit == 1) { for ( var i = (n == 1 ? 0 : 1); i <= 9; ++i) { val += countOfNumbers( digit + 1, i, prev1, n); } } // If current position is 2, // then any digit from [1-9] // can be placed. else if (digit == 2) { for ( var i = 0; i <= 9; ++i) { val += countOfNumbers( digit + 1, i, prev1, n); } } else { // previous digit selected is the mean. var mean = prev1; // mean = (current + prev2) / 2 // current = (2 * mean) - prev2 var current = (2 * mean) - prev2; // Check if current and current+1 // can be valid placements if (current >= 0 && current <= 9) val += countOfNumbers(digit + 1, current, prev1, n); if ((current + 1) >= 0 && (current + 1) <= 9) val += countOfNumbers(digit + 1, current + 1, prev1, n); } // return answer return val; } // Driver code // Given Input var n = 2; // Function call document.write(countOfNumbers(1, 0, 0, n)); // This code is contributed by gauravrajput1 </script> |
90
Time Complexity: O(N × 102)
Auxiliary Space: O(N × 102)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!