Given two arrays **arr1[]** and **arr2[]**. The task is to count the distinct sums that can be obtained while choosing a prime element from **arr1[]** and another prime element from **arr2[]**.

**Examples:**

Input:arr1[] = {2, 3}, arr2[] = {2, 2, 4, 7}

Output:4

All possible prime pairs are (2, 2), (2, 2), (2, 7), (3, 2), (3, 2)

and (3, 7) with sums 4, 4, 9, 5, 5 and 10 respectively.

Input:arr1[] = {3, 1, 4, 2, 5}, arr2[] = {8, 7, 10, 6, 5}

Output:5

**Approach:** Use Sieve of Eratosthenes to check whether a number is prime or not then for every prime pair, store it’s sum in a set in order to avoid duplicates. The size of the final set will be the required answer.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `#define MAX 1000000 ` `using` `namespace` `std; ` ` ` `bool` `prime[MAX]; ` `void` `sieve() ` `{ ` ` ` `memset` `(prime, ` `true` `, ` `sizeof` `(prime)); ` ` ` `prime[0] = prime[1] = ` `false` `; ` ` ` `for` `(` `int` `p = 2; p * p <= MAX; p++) { ` ` ` `if` `(prime[p] == ` `true` `) { ` ` ` `for` `(` `int` `i = p * p; i <= MAX; i += p) ` ` ` `prime[i] = ` `false` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to return the distinct sums ` `// that can be obtained by adding prime ` `// numbers from the given arrays ` `int` `distinctSum(` `int` `arr1[], ` `int` `arr2[], ` `int` `m, ` `int` `n) ` `{ ` ` ` `sieve(); ` ` ` ` ` `// Set to store distinct sums ` ` ` `set<` `int` `, greater<` `int` `> > sumSet; ` ` ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `for` `(` `int` `j = 0; j < n; j++) ` ` ` `if` `(prime[arr1[i]] && prime[arr2[j]]) ` ` ` `sumSet.insert(arr1[i] + arr2[j]); ` ` ` ` ` `return` `sumSet.size(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr1[] = { 2, 3 }; ` ` ` `int` `arr2[] = { 2, 2, 4, 7 }; ` ` ` `int` `m = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` `int` `n = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]); ` ` ` `cout << distinctSum(arr1, arr2, m, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `MAX = ` `1000000` `; ` ` ` `static` `boolean` `[]prime = ` `new` `boolean` `[MAX + ` `1` `]; ` `static` `void` `sieve() ` `{ ` ` ` `Arrays.fill(prime, ` `true` `); ` ` ` `prime[` `0` `] = prime[` `1` `] = ` `false` `; ` ` ` `for` `(` `int` `p = ` `2` `; p * p <= MAX; p++) ` ` ` `{ ` ` ` `if` `(prime[p] == ` `true` `) ` ` ` `{ ` ` ` `for` `(` `int` `i = p * p; ` ` ` `i <= MAX; i += p) ` ` ` `prime[i] = ` `false` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to return the distinct sums ` `// that can be obtained by adding prime ` `// numbers from the given arrays ` `static` `int` `distinctSum(` `int` `arr1[], ` ` ` `int` `arr2[], ` ` ` `int` `m, ` `int` `n) ` `{ ` ` ` `sieve(); ` ` ` ` ` `// Set to store distinct sums ` ` ` `Set<Integer> sumSet = ` `new` `HashSet<Integer>(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < m; i++) ` ` ` `for` `(` `int` `j = ` `0` `; j < n; j++) ` ` ` `if` `(prime[arr1[i]] && prime[arr2[j]]) ` ` ` `sumSet.add(arr1[i] + arr2[j]); ` ` ` ` ` `return` `sumSet.size(); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr1[] = { ` `2` `, ` `3` `}; ` ` ` `int` `arr2[] = { ` `2` `, ` `2` `, ` `4` `, ` `7` `}; ` ` ` `int` `m = arr1.length; ` ` ` `int` `n = arr2.length; ` ` ` `System.out.println(distinctSum(arr1, arr2, m, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `MAX` `=` `1000000` ` ` `prime ` `=` `[` `True` `for` `i ` `in` `range` `(` `MAX` `+` `1` `)] ` ` ` `def` `sieve(): ` ` ` ` ` `prime[` `0` `], prime[` `1` `] ` `=` `False` `, ` `False` ` ` ` ` `for` `p ` `in` `range` `(` `2` `, ` `MAX` `+` `1` `): ` ` ` `if` `p ` `*` `p > ` `MAX` `: ` ` ` `break` ` ` `if` `(prime[p] ` `=` `=` `True` `): ` ` ` `for` `i ` `in` `range` `(` `2` `*` `p, ` `MAX` `+` `1` `, p): ` ` ` `prime[i] ` `=` `False` ` ` `# Function to return the distinct sums ` `# that can be obtained by adding prime ` `# numbers from the given arrays ` `def` `distinctSum(arr1, arr2, m, n): ` ` ` `sieve() ` ` ` ` ` `# Set to store distinct sums ` ` ` `sumSet ` `=` `dict` `() ` ` ` ` ` `for` `i ` `in` `range` `(m): ` ` ` `for` `j ` `in` `range` `(n): ` ` ` `if` `(prime[arr1[i]] ` `and` ` ` `prime[arr2[j]]): ` ` ` `sumSet[arr1[i] ` `+` `arr2[j]] ` `=` `1` ` ` ` ` `return` `len` `(sumSet) ` ` ` `# Driver code ` `arr1 ` `=` `[` `2` `, ` `3` `] ` `arr2 ` `=` `[` `2` `, ` `2` `, ` `4` `, ` `7` `] ` `m ` `=` `len` `(arr1) ` `n ` `=` `len` `(arr2) ` `print` `(distinctSum(arr1, arr2, m, n)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` `static` `int` `MAX = 1000000; ` ` ` `static` `bool` `[]prime = ` `new` `bool` `[MAX + 1]; ` `static` `void` `sieve() ` `{ ` ` ` `for` `(` `int` `i = 0; i < MAX + 1; i++) ` ` ` `prime[i] = ` `true` `; ` ` ` `prime[0] = prime[1] = ` `false` `; ` ` ` `for` `(` `int` `p = 2; p * p <= MAX; p++) ` ` ` `{ ` ` ` `if` `(prime[p] == ` `true` `) ` ` ` `{ ` ` ` `for` `(` `int` `i = p * p; ` ` ` `i <= MAX; i += p) ` ` ` `prime[i] = ` `false` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to return the distinct sums ` `// that can be obtained by adding prime ` `// numbers from the given arrays ` `static` `int` `distinctSum(` `int` `[]arr1, ` ` ` `int` `[]arr2, ` ` ` `int` `m, ` `int` `n) ` `{ ` ` ` `sieve(); ` ` ` ` ` `// Set to store distinct sums ` ` ` `HashSet<` `int` `> sumSet = ` `new` `HashSet<` `int` `>(); ` ` ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `for` `(` `int` `j = 0; j < n; j++) ` ` ` `if` `(prime[arr1[i]] && prime[arr2[j]]) ` ` ` `sumSet.Add(arr1[i] + arr2[j]); ` ` ` ` ` `return` `sumSet.Count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr1 = { 2, 3 }; ` ` ` `int` `[]arr2 = { 2, 2, 4, 7 }; ` ` ` `int` `m = arr1.Length; ` ` ` `int` `n = arr2.Length; ` ` ` `Console.WriteLine(distinctSum(arr1, arr2, m, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count of pairs between two arrays such that the sums are distinct
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Print the nearest prime number formed by adding prime numbers to N
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Number of Co-prime pairs obtained from the sum of digits of elements in the given range
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count array elements that can be maximized by adding any permutation of first N natural numbers
- Check if sum Y can be obtained from the Array by the given operations
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Check if a prime number can be expressed as sum of two Prime Numbers
- Count all prime numbers in a given range whose sum of digits is also prime
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Count distinct elements after adding each element of First Array with Second Array
- Numbers less than N which are product of exactly two distinct prime numbers
- Find maximum points which can be obtained by deleting elements from array
- Maximum array sum that can be obtained after exactly k changes
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Find two distinct prime numbers with given product

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.