Count of arrays having consecutive element with different values | Javascript program to find count of arrays . ; static int MAXN = 109 ; ; Initialising dp [ 0 ] and dp [ 1 ] . ; Computing f ( i ) for each 2 <= i <= n . ; Driver code
let MAXN = 109 ; function countarray ( n , k , x ) { let dp = [ ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( let i = 2 ; i < n ; i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ; return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ; } let n = 4 , k = 3 , x = 2 ; document . write ( countarray ( n , k , x ) ) ;