Count permutation such that sequence is non decreasing | Javascript implementation of the approach ; To store the factorials ; Function to update fact [ ] array such that fact [ i ] = i ! ; 0 ! = 1 ; i ! = i * ( i - 1 ) ! ; Function to return the count of possible permutations ; To store the result ; Sort the array ; Initial size of the block ; Increase the size of block ; Update the result for the previous block ; Reset the size to 1 ; Update the result for the last block ; Driver code ; Pre - calculating factorials
const N = 20 ; let fact = new Array ( N ) ; function pre ( ) { fact [ 0 ] = 1 ; for ( let i = 1 ; i < N ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } } function CountPermutation ( a , n ) { let ways = 1 ; a . sort ( ) ; let size = 1 ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { size ++ ; } else { ways *= fact [ size ] ; size = 1 ; } } ways *= fact [ size ] ; return ways ; } let a = [ 1 , 2 , 4 , 4 , 2 , 4 ] ; let n = a . length ; pre ( ) ; document . write ( CountPermutation ( a , n ) ) ;