Count of subsequences which consists exactly K prime numbers | Returns factorial of n ; Function to return total number of combinations ; Function check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for finding number of subsequences which consists exactly K primes ; if number of primes are less thn k ; Driver code
function fact ( n ) { var res = 1 ; for ( var i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function nCr ( n , r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } function isPrime ( n ) { if ( n <= 1 ) return false ; for ( var i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function countSubsequences ( arr , n , k ) { var countPrime = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( isPrime ( arr [ i ] ) ) countPrime ++ ; } if ( countPrime < k ) return 0 ; return nCr ( countPrime , k ) * Math . pow ( 2 , ( n - countPrime ) ) ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; var K = 3 ; var n = arr . length ; document . write ( countSubsequences ( arr , n , K ) ) ;