Given an array arr of length N and an integer K, the task is to count the number of possible subsequences of length at most K which contains distinct prime elements from the array.
Examples:
Input: arr[] = {1, 2, 2, 3, 3, 4, 5}, N = 7, K = 3
Output: 18
Explanation: {}, {2}, {2}, {3}, {3}, {5}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 5}, {2, 5}, {3, 5}, {3, 5}, {2, 3, 5}, {2, 3, 5}, {2, 3, 5} and {2, 3, 5} are the subsequences.Input arr[] = {2, 4, 6, 7, 3, 9, 11, 5}, N = 8, K = 3
Output: 26
Approach:
Using Sieve of Eratosthenes, precompute and store all prime numbers. Calculate and store the frequency of every prime number in the given array. Using a dynamic programming approach, calculate the number of subsequences of length 2 to K. Keep updating ans by adding the possible distinct combinations of length 2 to K for each dp[i]. Once calculated, add the frequency of all the primes + 1 as the subsequences of length 1 and 0. The final value of ans gives the desired result.
Below is the implementation of the above approach:
C++
// C++ Program to find the // count of distinct prime // subsequences at most of // of length K from a given array #include <bits/stdc++.h> using namespace std; bool prime[100001]; void SieveOfEratosthenes() { // Initialize all indices as true memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false; // A value in prime[i] will finally be // false if i is not a prime, else true for (int p = 2; p * p <= 100000; p++) { // If prime[p] is true, // then it is a prime if (prime[p] == true) { // Update all multiples of p // as false, i.e. non-prime for (int i = p * p; i <= 100000; i += p) prime[i] = false; } } } // Returns number of subsequences // of maximum length k and // contains distinct primes int distinctPrimeSubSeq( int a[], int n, int k) { SieveOfEratosthenes(); // Store the primes in // the given array vector<int> primes; for (int i = 0; i < n; i++) { if (prime[a[i]]) primes.push_back(a[i]); } int l = primes.size(); // Sort the primes sort(primes.begin(), primes.end()); // Store the frequencies // of all the // distinct primes vector<int> b; vector<int> dp; int sum = 0; for (int i = 0; i < l;) { int count = 1, x = a[i]; i++; while (i < l && a[i] == x) { count++; i++; } // Store the frequency // of primes b.push_back(count); dp.push_back(count); // Store the sum of all // frequencies sum += count; } // Store the length of // subsequence at every // instant int of_length = 2; int len = dp.size(); int ans = 0; while (of_length <= k) { // Store the frequency int freq = 0; // Store the previous // count of updated DP int prev = 0; for (int i = 0; i < (len - 1); i++) { freq += dp[i]; int j = sum - freq; // Calculate total subsequences // of current of_length int subseq = b[i] * j; // Add the number of // subsequences to the answer ans += subseq; // Update the value in dp[i] dp[i] = subseq; // Store the updated dp[i] prev += dp[i]; } len--; sum = prev; of_length++; } ans += (l + 1); return ans; } // Driver Code int main() { int a[] = { 1, 2, 2, 3, 3, 4, 5 }; int n = sizeof(a) / sizeof(int); int k = 3; cout << distinctPrimeSubSeq(a, n, k); return 0; } |
18
Time Complexity: O(K*(Distinct number of prime))
Recommended Posts:
- Count of subsequences having maximum distinct elements
- Count Distinct Subsequences
- Length of Longest Subarray with same elements in atmost K increments
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count of subsequences which consists exactly K prime numbers
- Count of subsequences of length 4 in form (x, x, x+1, x+1) | Set 2
- Count unique subsequences of length K
- Minimize sum of smallest elements from K subsequences of length L
- Count the number of subsequences of length k having equal LCM and HCF
- Count of possible subarrays and subsequences using given length of Array
- Number of subsequences of maximum length K containing no repeated elements
- Length of array pair formed where one contains all distinct elements and other all same elements
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Count of distinct power of prime factor of N
- Count of all subsequences having adjacent elements with different parity
- Check whether count of distinct characters in a string is Prime or not
- Count number of distinct substrings of a given length
- Count of substrings of length K with exactly K distinct characters
- Sort an array according to the increasing count of distinct Prime Factors
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
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.


