Count of N digit Numbers having no pair of equal consecutive Digits | Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Calculate the total count of valid ( i - 1 ) - digit numbers ; Update dp [ ] [ ] table ; Calculate the count of required N - digit numbers ; Driver Code
function count ( N ) { if ( N == 1 ) { document . write ( ( 10 ) + " " ) ; return ; } var dp = Array . from ( Array ( N ) , ( ) => Array ( 10 ) . fill ( 0 ) ) ; for ( var i = 1 ; i < 10 ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( var i = 1 ; i < N ; i ++ ) { var temp = 0 ; for ( var j = 0 ; j < 10 ; j ++ ) temp += dp [ i - 1 ] [ j ] ; for ( var j = 0 ; j < 10 ; j ++ ) dp [ i ] [ j ] = temp - dp [ i - 1 ] [ j ] ; } var ans = 0 ; for ( var i = 0 ; i < 10 ; i ++ ) ans += dp [ N - 1 ] [ i ] ; document . write ( ans ) ; } var N = 2 ; count ( N ) ;