Count of arrays having consecutive element with different values | Return the number of lists with given constraints . ; Initialising dp [ 0 ] and dp [ 1 ] ; Computing f ( i ) for each 2 <= i <= n . ; Driven code
def countarray ( n , k , x ) : NEW_LINE INDENT dp = list ( ) NEW_LINE dp . append ( 0 ) NEW_LINE dp . append ( 1 ) NEW_LINE i = 2 NEW_LINE while i < n : NEW_LINE INDENT dp . append ( ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ) NEW_LINE i = i + 1 NEW_LINE DEDENT return ( ( k - 1 ) * dp [ n - 2 ] if x == 1 else dp [ n - 1 ] ) NEW_LINE DEDENT n = 4 NEW_LINE k = 3 NEW_LINE x = 2 NEW_LINE print ( countarray ( n , k , x ) ) NEW_LINE