- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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.

#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[0]); int len2=sizeof(Arr2) / sizeof(Arr2[0]); cout<<"Distinct Sums of primes :"<<prime_Sums(Arr1,Arr2,len1,len2); return 0; }

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

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

- Related Questions & Answers
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Count unique numbers that can be generated from N by adding one and removing trailing zeros in C++
- Count of numbers which can be made power of 2 by given operation in C++
- Find two distinct prime numbers with given product in C++
- Count occurrences of a string that can be constructed from another given string in C++
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Find maximum points which can be obtained by deleting elements from array in C++
- Count pairs formed by distinct element sub-arrays in C++
- Find two distinct prime numbers with given product in C++ Program
- Print the nearest prime number formed by adding prime numbers to N
- Maximum array sum that can be obtained after exactly k changes in C++
- First N natural can be divided into two sets with given difference and co-prime sums in C++
- Find maximum points which can be obtained by deleting elements from array in Python
- Count distinct pairs from two arrays having same sum of digits in C++
- Find count of Almost Prime numbers from 1 to N in C++

Advertisements