

- 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
Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++
<p>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.</p><p>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.</p><p>Let’s understand with examples.</p><p><strong>Input</strong> </p><pre class="result notranslate">Arr1[] = { 1,2,3 } Arr2[] = { 2,3,4}</pre><p><strong>Output</strong> </p><pre class="result notranslate">Distinct Sums of primes :3</pre><p><strong>Explanation</strong> </p><pre class="result notranslate">Prime pairs (2,2), (2,3), (3,2), (3,3). Unique sums are 4,5,6</pre><p><strong>Input</strong> </p><pre class="result notranslate">Arr1[] = { 1,4,6 } Arr2[] = { 2,3,5 }</pre><p><strong>Output</strong> </p><pre class="result notranslate">Distinct Sums of primes :0</pre><p><strong>Explanation</strong> </p><pre class="result notranslate">Arr1[] has no prime number. Prime pairs do not exist.</pre><h2>Approach used in the below program is as follows</h2><ul class="list"><li><p>We have two arrays Arr1[] and Arr2[] for positive numbers and len1 and len2 as their lengths.</p></li><li><p>Function isprime(int num) returns 1 if num is prime otherwise returns 0.</p></li><li><p>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.</p></li><li><p>Take a set<int> sum to store distinct sums.</p></li><li><p>Traverse each element of both arrays usin for loops.</p></li><li><p>Check if isprime(arr1[i]) && isprime(arr2[j]). If true, take sum as tmp=arr1[i]+arr2[j].</p></li><li><p>Add tmp to set using sum.insert(tmp)</p></li><li><p>At the end return sum.size() as result which is distinct sums of prime numbers.</p></li></ul><h2>Example</h2><!--<a href="http://tpcg.io/cUNxsOwC" target="_blank" rel="nofollow" class="demo"><i class="fa-external-link"></i> Live Demo</a></p>--><pre class="prettyprint notranslate">#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; }</pre><h2>Output</h2><p>If we run the above code it will generate the following output −</p><pre class="result notranslate">Count of ways to spell a number with repeated digits are: 16</pre>
- 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++
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Count of numbers which can be made power of 2 by given operation in C++
- Print the nearest prime number formed by adding prime numbers to N
- Count occurrences of a string that can be constructed from another given string in C++
- Find two distinct prime numbers with given product in C++
- Find maximum points which can be obtained by deleting elements from array in Python
- Find maximum points which can be obtained by deleting elements from array in C++
- Find two distinct prime numbers with given product in C++ Program
- Count pairs formed by distinct element sub-arrays in C++
- Python - Find the latest valid time that can be obtained by replacing the unknown/hidden digits
- First N natural can be divided into two sets with given difference and co-prime sums in C++
- Maximum array sum that can be obtained after exactly k changes in C++
- Count distinct pairs from two arrays having same sum of digits in C++
Advertisements