# Count of subsequences which consists exactly K prime numbers

Given an integer **K** and an array **arr[]**, the task is to find the number of subsequences from the given array such that each subsequence consists exactly K prime numbers.

**Example:**

Input:K = 2, arr = [2, 3, 4, 6]

Output:4

Explanation:

There are 4 subsequences which consists exaclty 2 prime numbers {2, 3} {2, 3, 4} {2, 3, 6} {2, 3, 4, 6}

Input:K = 3, arr = [1, 2, 3, 4, 5, 6, 7]

Output:32

Explanation:

There are 32 subsequences which consists exaclty 3 prime numbers.

**Approach:** To solve the problem mentioned above we have to find the count of prime numbers in the given array.

- Let the count of prime numbers is
*m*. - We can choose any K integers among
*m*prime numbers. - So the possible combination of choosing prime numbers in the subsequence is and in the subsequence we can add any number of non – prime numbers because there is no restriction on the non prime numbers where count of non prime numbers will be (N – m).
- We can choose any subset of the (N – m) for nonprime numbers in our subsequence.
- Possibility of choosing all subset of size (N – m) is
**pow(2, (m – N))**. - For generating subsequences, we will multiply prime number possibility with nonprime number possibility.

Sub sequence count = (Count of prime numbers) C (K) * pow(2, count of non-prime numbers)

Below is the implementation of the above approach:

`// C++ implementation to find ` `// the count of subsequences ` `// which consist exactly K primes ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns factorial of n ` `int` `fact(` `int` `n) ` `{ ` ` ` `int` `res = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `res = res * i; ` ` ` `return` `res; ` `} ` ` ` `// Function to return total ` `// number of combinations ` `int` `nCr(` `int` `n, ` `int` `r) ` `{ ` ` ` `return` `fact(n) ` ` ` `/ (fact(r) ` ` ` `* fact(n - r)); ` `} ` ` ` `// Function check whether a number ` `// is prime or not ` `bool` `isPrime(` `int` `n) ` `{ ` ` ` `// Corner case ` ` ` `if` `(n <= 1) ` ` ` `return` `false` `; ` ` ` ` ` `// Check from 2 to n-1 ` ` ` `for` `(` `int` `i = 2; i < n; i++) ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function for finding number of subsequences ` `// which consists exactly K primes ` `int` `countSubsequences(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `countPrime = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(isPrime(arr[i])) ` ` ` `countPrime++; ` ` ` `} ` ` ` `// if number of primes are less thn k ` ` ` `if` `(countPrime < k) ` ` ` `return` `0; ` ` ` ` ` `return` `nCr(countPrime, k) ` ` ` `* ` `pow` `(2, (n - countPrime)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 1, 2, 3, 4, 5, 6, 7 }; ` ` ` `int` `K = 3; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << countSubsequences(arr, n, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

32

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count Numbers with N digits which consists of odd number of 0's
- Count Numbers with N digits which consists of even number of 0’s
- Count of subsequences of length atmost K containing distinct prime elements
- Count all prime numbers in a given range whose sum of digits is also prime
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Count of subarrays having exactly K prime numbers
- Sum of all the prime numbers with the count of digits ≤ D
- Find count of Almost Prime numbers from 1 to N
- Count of numbers below N whose sum of prime divisors is K
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count of natural numbers in range [L, R] which are relatively prime with N
- Count numbers from range whose prime factors are only 2 and 3
- Count of Double Prime numbers in a given range L to R
- Count common prime factors of two numbers
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Queries for the difference between the count of composite and prime numbers in a given range
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.