Count possible permutations of given array satisfying the given conditions | Function to get the value of binomial coefficient ; Stores the value of binomial coefficient ; Since C ( N , R ) = C ( N , N - R ) ; Calculate the value of C ( N , R ) ; Function to get the count of permutations of the array that satisfy the condition ; Stores the value of C ( 2 N , N ) ; Stores the value of catalan number ; Return answer ; Driver Code
def binCoff ( N , R ) : NEW_LINE INDENT res = 1 NEW_LINE if ( R > ( N - R ) ) : NEW_LINE INDENT R = ( N - R ) NEW_LINE DEDENT for i in range ( R ) : NEW_LINE INDENT res *= ( N - i ) NEW_LINE res //= ( i + 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT def cntPermutation ( N ) : NEW_LINE INDENT C_2N_N = binCoff ( 2 * N , N ) NEW_LINE cntPerm = C_2N_N // ( N + 1 ) NEW_LINE return cntPerm NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntPermutation ( N // 2 ) ) NEW_LINE DEDENT