Count of integers having difference with its reverse equal to D | Maximum digits in N ; Utility function to find count of N such that N + D = reverse ( N ) ; If d is a multiple of 9 , no such value N found ; Divide D by 9 check reverse of number and its sum ; B [ i ] : Stores power of ( 10 , i ) ; Calculate power ( 10 , i ) ; Update B [ i ] ; Stores count of N such that N + D = reverse ( N ) ; Iterate over the range [ 1 , MAXL ] ; Update ans ; Function to find count of possible values of N such that N + D = reverse ( N ) ; Base Case ; If D is not qual to 0 ; Stores count of possible values of N such that N + D = reverse ( N ) ; Update ans ; If l is even ; Update ans ; Stores count of possible values of N such that N + D = reverse ( N ) ; Iterae over the range [ - 9 , 9 ] ; Update ans ; Driver Code ; Function call
MAXL = 17 ; NEW_LINE N = 0 ; NEW_LINE v = 0 ; NEW_LINE def findN ( D ) : NEW_LINE INDENT global N ; NEW_LINE global v ; NEW_LINE if ( D % 9 != 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT D //= 9 ; NEW_LINE B = [ 0 ] * MAXL ; NEW_LINE B [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , MAXL ) : NEW_LINE INDENT B [ i ] = B [ i - 1 ] * 10 ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( 1 , MAXL + 1 ) : NEW_LINE INDENT N = ( i + 1 ) // 2 ; NEW_LINE v = [ 0 ] * N ; NEW_LINE for j in range ( N ) : NEW_LINE INDENT for k in range ( j , i - j ) : NEW_LINE INDENT v [ j ] += B [ k ] ; NEW_LINE DEDENT DEDENT temp = [ 0 ] * N ; NEW_LINE ans += count ( D , i , 0 , temp ) ; NEW_LINE return ans ; NEW_LINE DEDENT DEDENT def count ( D , l , t , x ) : NEW_LINE INDENT global N ; NEW_LINE global v ; NEW_LINE if ( t == N ) : NEW_LINE INDENT if ( D != 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT ans = 1 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans *= ( 9 if i == 0 else 10 ) - abs ( x [ i ] ) ; NEW_LINE DEDENT if ( l % 2 == 0 ) : NEW_LINE INDENT ans *= 10 ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for m in range ( - 9 , 10 ) : NEW_LINE INDENT if ( - v [ t ] < D + v [ t ] * m and D + v [ t ] * m < v [ t ] ) : NEW_LINE INDENT x [ t ] = m ; NEW_LINE ans += count ( D + v [ t ] * m , l , t + 1 , x ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT D = 63 ; NEW_LINE print ( findN ( D ) ) ; NEW_LINE DEDENT