Count of arrays having consecutive element with different values | Java program to find count of arrays . ; Return the number of arrays with given constartints . ; Initialising dp [ 0 ] and dp [ 1 ] . ; Computing f ( i ) for each 2 <= i <= n . ; driver code
import java . util . * ; class Counting { static int MAXN = 109 ; public static int countarray ( int n , int k , int x ) { int [ ] dp = new int [ 109 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int 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 ] ) ; } public static void main ( String [ ] args ) { int n = 4 , k = 3 , x = 2 ; System . out . println ( countarray ( n , k , x ) ) ; } }