Problem: Length of longest common prime subsequence from two given arrays
Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to find the length of the longest common prime subsequence that can be obtained from the two given arrays.

Sample Input: arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr2[] = {2, 5, 6, 3, 7, 9, 8}
Sample Output: 4
Explanation:
The longest common prime subsequence present in both the arrays is {2, 3, 5, 7}.

Solution (cpp):
// CPP implementation of the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to calculate the LCS
int recursion(vector<int> arr1,
              vector<int> arr2, int i,
              int j, map<pair<int, int>, 
              int> dp)
{
    if (i >= arr1.size() or j >= arr2.size())
        return 0;
    pair<int, int> key = { i, j };
    if (arr1[i] == arr2[j])
        return 1 
               + recursion(arr1, arr2, 
                            i + 1, j + 1,
                           dp);
    if (dp.find(key) != dp.end())
        return dp[key];

    else
        dp[key] = max(recursion(arr1, arr2, 
                                i + 1, j, dp),
                      recursion(arr1, arr2, i, 
                                j + 1, dp));
    return dp[key];
}

// Function to generate
// all the possible
// prime numbers
vector<int> primegenerator(int n)
{
    int cnt = 0;
    vector<int> primes(n + 1, true);
    int p = 2;
    while (p * p <= n)
    {
        for (int i = p * p; i <= n; i += p)
            primes[i] = false;
        p += 1;
    }
    return primes;
}

// Function which returns the
// length of longest common
// prime subsequence
int longestCommonSubseq(vector<int> arr1, 
                        vector<int> arr2)
{

    // Minimum element of
    // both arrays
    int min1 = *min_element(arr1.begin(), 
                            arr1.end());
    int min2 = *min_element(arr2.begin(),
                            arr2.end());

    // Maximum element of
    // both arrays
    int max1 = *max_element(arr1.begin(),
                            arr1.end());
    int max2 = *max_element(arr2.begin(), 
                            arr2.end());

    // Generating all primes within
    // the max range of arr1
    vector<int> a = primegenerator(max1);

    // Generating all primes within
    // the max range of arr2
    vector<int> b = primegenerator(max2);

    vector<int> finala;
    vector<int> finalb;

    // Store precomputed values
    map<pair<int, int>, int> dp;

    // Store all primes in arr1[]
    for (int i = min1; i <= max1; i++) 
    {
        if (find(arr1.begin(), arr1.end(), i) 
            != arr1.end()
            and a[i] == true)
            finala.push_back(i);
    }

    // Store all primes of arr2[]
    for (int i = min2; i <= max2; i++) 
    {
        if (find(arr2.begin(), arr2.end(), i) 
            != arr2.end()
            and b[i] == true)
            finalb.push_back(i);
    }

    // Calculating the LCS
    return recursion(finala, finalb, 0, 0, dp);
}

// Driver Code
int main()
{
    vector<int> arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    vector<int> arr2 = { 2, 5, 6, 3, 7, 9, 8 };
    
    // Function Call
    cout << longestCommonSubseq(arr1, arr2);
}