Count of subsequences of length atmost K containing distinct prime elements

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

18

Time Complexity: O(K*(Distinct number of prime))

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.