Count of N | python program for the above approach ; Function to find number of ' N ' digit numbers such that the element is mean of sum of its adjacent digits ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If current position is 1 , then any digit from [ 1 - 9 ] can be placed . If n = 1 , 0 can be also placed . ; If current position is 2 , then any digit from [ 1 - 9 ] can be placed . ; previous digit selected is the mean . ; mean = ( current + prev2 ) / 2 current = ( 2 * mean ) - prev2 ; Check if current and current + 1 can be valid placements ; return answer ; Given Input ; Function call
dp = [ [ [ - 1 for i in range ( 10 ) ] for col in range ( 20 ) ] for row in range ( 100 ) ] NEW_LINE def countOfNumbers ( digit , prev1 , prev2 , n ) : NEW_LINE INDENT if ( digit == n + 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT val = dp [ digit ] [ prev1 ] [ prev2 ] NEW_LINE if ( val != - 1 ) : NEW_LINE INDENT return val NEW_LINE DEDENT val = 0 NEW_LINE if ( digit == 1 ) : NEW_LINE INDENT start = 1 NEW_LINE if ( n == 1 ) : NEW_LINE INDENT start = 0 NEW_LINE DEDENT for i in range ( start , 10 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , prev1 , n ) NEW_LINE DEDENT DEDENT elif ( digit == 2 ) : NEW_LINE INDENT for i in range ( 0 , 10 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , i , prev1 , n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT mean = prev1 NEW_LINE current = ( 2 * mean ) - prev2 NEW_LINE if ( current >= 0 and current <= 9 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , current , prev1 , n ) NEW_LINE DEDENT if ( ( current + 1 ) >= 0 and ( current + 1 ) <= 9 ) : NEW_LINE INDENT val += countOfNumbers ( digit + 1 , current + 1 , prev1 , n ) NEW_LINE DEDENT DEDENT return val NEW_LINE DEDENT n = 2 NEW_LINE print ( countOfNumbers ( 1 , 0 , 0 , n ) ) NEW_LINE