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 count of permutations of the array that satisfy the given condition ; Stores the value of C ( 2 N , N ) ; Stores the value of catalan number ; Return answer ; Driver Code
function binCoff ( N , R ) { var res = 1 ; if ( R > ( N - R ) ) { R = ( N - R ) ; } for ( i = 0 ; i < R ; i ++ ) { res *= ( N - i ) ; res /= ( i + 1 ) ; } return res ; } function cntPermutation ( N ) { var cntPerm ; var C_2N_N = binCoff ( 2 * N , N ) ; cntPerm = C_2N_N / ( N + 1 ) ; return cntPerm ; } var arr = [ 1 , 2 , 3 , 4 ] ; var N = arr . length ; document . write ( cntPermutation ( N / 2 ) ) ;