Count the number of subsequences of length k having equal LCM and HCF | Returns factorial of n ; Returns nCr for the given values of r and n ; Map to store the frequencies of each elements ; Loop to store the frequencies of elements in the map ; Using nCR formula to calculate the number of subsequences of a given length ; Driver Code ; Function calling
function fact ( n ) { let res = 1 ; for ( let i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function nCr ( n , r ) { return fact ( n ) / ( 1 * fact ( r ) * fact ( n - r ) ) ; } function number_of_subsequences ( arr , k , n ) { let s = 0 ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } for ( let [ key , value ] of mp . entries ( ) ) { s = s + 1 * nCr ( value , k ) ; } return s ; } let arr = [ 1 , 1 , 1 , 1 , 2 , 2 , 2 ] ; let k = 2 ; let n = arr . length ; document . write ( number_of_subsequences ( arr , k , n ) ) ;