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

C++Server Side ProgrammingProgramming

<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&lt;int&gt; sums. In the end the size of the set is the number of distinct sums of primes.</p><p>Let&rsquo;s understand with examples.</p><p><strong>Input</strong>&nbsp;</p><pre class="result notranslate">Arr1[] = { 1,2,3 } Arr2[] = { 2,3,4}</pre><p><strong>Output</strong>&nbsp;</p><pre class="result notranslate">Distinct Sums of primes :3</pre><p><strong>Explanation</strong>&nbsp;</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>&nbsp;</p><pre class="result notranslate">Arr1[] = { 1,4,6 } Arr2[] = { 2,3,5 }</pre><p><strong>Output</strong>&nbsp;</p><pre class="result notranslate">Distinct Sums of primes :0</pre><p><strong>Explanation</strong>&nbsp;</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&lt;int&gt; 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]) &amp;&amp; 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="" target="_blank" rel="nofollow" class="demo"><i class="fa-external-link"></i> Live Demo</a></p>--><pre class="prettyprint notranslate">#include&lt;bits/stdc++.h&gt; using namespace std; int isprime(int num){ &nbsp; &nbsp;if (num &lt;= 1) &nbsp; &nbsp; &nbsp; return 0; &nbsp; &nbsp;for (int i = 2; i &lt;= num/2; i++) &nbsp; &nbsp; &nbsp; if (num % i == 0) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return 0; &nbsp; &nbsp;return 1; //if both failed then num is prime } int prime_Sums(int arr1[],int arr2[],int l1,int l2){ &nbsp; &nbsp;int count=0; &nbsp; &nbsp;set sum; &nbsp; &nbsp;for (int i = 0; i &lt; l1; i++){ &nbsp; &nbsp; &nbsp; for(int j=0; j &lt; l2; j++){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(isprime(arr1[i]) &amp;&amp; isprime(arr2[j])){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tmp=arr1[i]+arr2[j]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum.insert(tmp); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return sum.size(); } int main(){ &nbsp; &nbsp;int Arr1[] = { 2, 3, 5 }; &nbsp; &nbsp;int Arr2[] = { 2, 2, 4, 7 }; &nbsp; &nbsp;int len1=sizeof(Arr1) / sizeof(Arr1[0]); &nbsp; &nbsp;int len2=sizeof(Arr2) / sizeof(Arr2[0]); &nbsp; &nbsp;cout&lt;&lt;&quot;Distinct Sums of primes :&quot;&lt;&lt;prime_Sums(Arr1,Arr2,len1,len2); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>If we run the above code it will generate the following output &minus;</p><pre class="result notranslate">Count of ways to spell a number with repeated digits are: 16</pre>
Updated on 31-Oct-2020 05:44:50