Count of N | Function to find count of N - digit numbers with single digit XOR ; dp [ i ] [ j ] stores the number of i - digit numbers with XOR equal to j ; For 1 - 9 store the value ; Iterate till N ; Calculate XOR ; Store in DP table ; Initialize count ; Prvar the answer ; Given number N ; Function Call
function countNums ( N ) { var dp = Array ( N ) ; for ( var i = 0 ; i < N ; i ++ ) dp [ i ] = Array ( 16 ) . fill ( 0 ) ; for ( i = 1 ; i <= 9 ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( i = 1 ; i < N ; i ++ ) { for ( j = 0 ; j < 10 ; j ++ ) { for ( k = 0 ; k < 16 ; k ++ ) { var xor = j ^ k ; dp [ i ] [ xor ] += dp [ i - 1 ] [ k ] ; } } } var count = 0 ; for ( i = 0 ; i < 10 ; i ++ ) count += dp [ N - 1 ] [ i ] ; document . write ( count ) ; } var N = 1 ; countNums ( N ) ;