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
let MAXL = 17 ; let N ; let v = [ ] function findN ( D ) { if ( D % 9 != 0 ) return 0 ; D /= 9 ; let B = new Array ( MAXL ) . fill ( 0 ) ; B [ 0 ] = 1 ; for ( let i = 1 ; i < MAXL ; i ++ ) { B [ i ] = B [ i - 1 ] * 10 ; } let ans = 0 ; for ( let i = 1 ; i <= MAXL ; i ++ ) { N = Math . floor ( ( i + 1 ) / 2 ) ; v = new Array ( N ) . fill ( 0 ) ; for ( let j = 0 ; j < N ; j ++ ) for ( let k = j ; k < i - j ; k ++ ) v [ j ] += B [ k ] ; ans += count ( D , i , 0 , new Array ( N ) ) ; } return ans ; } function count ( D , l , t , x ) { if ( t == N ) { if ( D != 0 ) return 0 ; let ans = 1 ; for ( let i = 0 ; i < N ; i ++ ) { ans *= ( i == 0 ? 9 : 10 ) - Math . abs ( x [ i ] ) ; } if ( l % 2 == 0 ) { ans *= 10 ; } return ans ; } let ans = 0 ; for ( let m = - 9 ; m <= 9 ; m ++ ) { if ( - v [ t ] < D + v [ t ] * m && D + v [ t ] * m < v [ t ] ) { x [ t ] = m ; ans += count ( D + v [ t ] * m , l , t + 1 , x ) ; } } return ans ; } let D = 63 ; document . write ( findN ( D ) ) ;