# Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++

We are given two arrays containing prime and non prime numbers. The goal is to find the count of distinct sums of pairs of prime numbers in each array.

We will do this by making a pair of two primes from each array, take their sum and add them to set<int> sums. In the end the size of the set is the number of distinct sums of primes.

Let’s understand with examples.

Input

Arr1[] = { 1,2,3 } Arr2[] = { 2,3,4}

Output

Distinct Sums of primes :3

Explanation

Prime pairs (2,2), (2,3), (3,2), (3,3).
Unique sums are 4,5,6

Input

Arr1[] = { 1,4,6 } Arr2[] = { 2,3,5 }

Output

Distinct Sums of primes :0

Explanation

Arr1[] has no prime number. Prime pairs do not exist.

## Approach used in the below program is as follows

• We have two arrays Arr1[] and Arr2[] for positive numbers and len1 and len2 as their lengths.

• Function isprime(int num) returns 1 if num is prime otherwise returns 0.

• Function prime_Sums(int arr1[],int arr2[],int l1,int l2) takes both arrays and returns the count of distinct sums of pairs of prime.

• Take a set<int> sum to store distinct sums.

• Traverse each element of both arrays usin for loops.

• Check if isprime(arr1[i]) && isprime(arr2[j]). If true, take sum as tmp=arr1[i]+arr2[j].

• Add tmp to set using sum.insert(tmp)

• At the end return sum.size() as result which is distinct sums of prime numbers.

## Example

#include<bits/stdc++.h>
using namespace std;
int isprime(int num){
if (num <= 1)
return 0;
for (int i = 2; i <= num/2; i++)
if (num % i == 0)
return 0;
return 1; //if both failed then num is prime
}
int prime_Sums(int arr1[],int arr2[],int l1,int l2){
int count=0;
set sum;
for (int i = 0; i < l1; i++){
for(int j=0; j < l2; j++){
if(isprime(arr1[i]) && isprime(arr2[j])){
int tmp=arr1[i]+arr2[j];
sum.insert(tmp);
}
}
}
return sum.size();
}
int main(){
int Arr1[] = { 2, 3, 5 };
int Arr2[] = { 2, 2, 4, 7 };
int len1=sizeof(Arr1) / sizeof(Arr1);
int len2=sizeof(Arr2) / sizeof(Arr2);
cout<<"Distinct Sums of primes :"<<prime_Sums(Arr1,Arr2,len1,len2);
return 0;
}

## Output

If we run the above code it will generate the following output −

Count of ways to spell a number with repeated digits are: 16