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 (java):
// JAVA implementation of the above approach
import java.util.*;
import java.io.*;
import java.math.*;
public class InfobayAI
{

  // Function to calculate the LCS
  static int recursion(ArrayList<Integer> arr1,
                       ArrayList<Integer> arr2, int i,
                       int j, Map<ArrayList<Integer>, 
                       Integer> dp)
  {
    if (i >= arr1.size() || j >= arr2.size())
      return 0;
    ArrayList<Integer> key = new ArrayList<>();
    key.add(i);
    key.add(j);
    if (arr1.get(i) == arr2.get(j))
      return 1 + recursion(arr1, arr2, 
                           i + 1, j + 1,
                           dp);
    if (dp.get(key) != dp.get(dp.size()-1))
      return dp.get(key);

    else
      dp.put(key,Math.max(recursion(arr1, arr2, 
                                    i + 1, j, dp),
                          recursion(arr1, arr2, i, 
                                    j + 1, dp)));
    return dp.get(key);
  }

  // Function to generate
  // all the possible
  // prime numbers
  static ArrayList<Boolean> primegenerator(int n)
  {
    int cnt = 0;
    ArrayList<Boolean> primes = new ArrayList<>();
    for(int i = 0; i < n + 1; i++)
      primes.add(true);
    int p = 2;
    while (p * p <= n)
    {
      for (int i = p * p; i <= n; i += p)
        primes.set(i,false);
      p += 1;
    }
    return primes;
  }

  // Function that returns the Minimum element of an ArrayList
  static int min_element(ArrayList<Integer> al)
  {
    int min = Integer.MAX_VALUE;
    for(int i = 0; i < al.size(); i++)
    {
      min=Math.min(min, al.get(i));
    }
    return min;
  }

  // Function that returns the Minimum element of an ArrayList
  static int max_element(ArrayList<Integer> al)
  {
    int max = Integer.MIN_VALUE;
    for(int i = 0; i < al.size(); i++)
    {
      max = Math.max(max, al.get(i));
    }
    return max;
  }

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

    // Minimum element of
    // both arrays
    int min1 = min_element(arr1);
    int min2 = min_element(arr2);

    // Maximum element of
    // both arrays
    int max1 = max_element(arr1);
    int max2 = max_element(arr2);

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

    // Generating all primes within
    // the max range of arr2
    ArrayList<Boolean> b = primegenerator(max2);
    ArrayList<Integer> finala = new ArrayList<>();
    ArrayList<Integer> finalb = new ArrayList<>();

    // Store precomputed values
    Map<ArrayList<Integer>,Integer> dp = 
      new HashMap <ArrayList<Integer>,Integer> ();

    // Store all primes in arr1[]
    for (int i = min1; i <= max1; i++) 
    {
      if (arr1.contains(i)
          && a.get(i) == true)
        finala.add(i);
    }

    // Store all primes of arr2[]
    for (int i = min2; i <= max2; i++) 
    {
      if (arr2.contains(i)
          && b.get(i) == true)
        finalb.add(i);
    }

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

  // Driver Code
  public static void main(String args[])
  {
    int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int a2[] = { 2, 5, 6, 3, 7, 9, 8 };

    // Converting into list
    ArrayList<Integer> arr1 = new ArrayList<Integer>();
    for(int i = 0; i < a1.length; i++)
      arr1.add(a1[i]);

    ArrayList<Integer> arr2 = new ArrayList<Integer>();
    for(int i = 0; i < a2.length; i++)
      arr2.add(a2[i]);

    // Function Call
    System.out.println(longestCommonSubseq(arr1, arr2));
  }
}