Count distinct pairs from two arrays having same sum of digits in C++

C++Server Side ProgrammingProgramming

<p>We are given with two arrays let&rsquo;s say, arr_1[] and arr_2[] having integer values and the task is to calculate the count of distinct pairs having the same sum of digits. It means, one value should be selected from an arr_1[] and second value from arr_2[] to form a pair and both the values should have the same sum digits.</p><p>Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.</p><h2>For Example</h2><pre class="result notranslate"><strong>Input</strong> &minus; int arr_1[] = {1, 22, 42, 17} &nbsp; &nbsp;Int arr_2[] = {1, 31, 6, 8} <strong>Output</strong> &minus; count is 4</pre><p><strong>Explanation</strong> &minus; in total there are 4 pairs having the same sum of digits and those are (1, 1), (22, 31), (42, 6) and (17, 8).</p><pre class="result notranslate"><strong>Input</strong> &minus; int arr_1[] = {1, 22, 42, 17} &nbsp; &nbsp;Int arr_2[] = {2, 78, 6, 18} <strong>Output</strong> &minus; count is 1</pre><p><strong>Explanation</strong> &minus; in total there is only one pair having the same sum of digits and that is (42, 6).</p><pre class="result notranslate"><strong>Input</strong> &minus; int arr_1[] = {1, 22, 42, 17} &nbsp; &nbsp;Int arr_2[] = {2, 78, 16, 18} <strong>Output</strong> &minus; count is 0</pre><p><strong>Explanation</strong> &minus; There is no pair having the same sum of digits so count is 0.</p><h2>Approach used in the below program is as follows &minus;</h2><ul class="list"><li><p>Create two arrays let&rsquo;s say, arr_1[] and arr_2[]</p></li><li><p>Calculate the length of both the arrays using the length() function that will return an integer value as per the elements in an array.</p></li><li><p>Create a set type variable let&rsquo;s say st</p></li><li><p>Start loop for i to 0 and i less than size of arr_1[]</p></li><li><p>Inside the loop, Start another loop for j to 0 and j less than size of arr_2[].</p></li><li><p>Check if Sum(arr[i]) = sum(arr_2[j]) then check if arr_1[i] less than arr_2[j] then insert(make_pair(arr_1[i], arr_2[j])</p></li><li><p>Else, insert(make_pair(arr_2[j], arr_1[i]).</p></li><li><p>Return st.size()</p></li><li><p>Print the result.</p></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/kSEd8k7L" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;iostream&gt; #include &lt;set&gt; using namespace std; // Function to find the // sum of digits of a number int sumdigits(int n){ &nbsp; &nbsp;int sum = 0; &nbsp; &nbsp;while (n &gt; 0){ &nbsp; &nbsp; &nbsp; sum += n % 10; &nbsp; &nbsp; &nbsp; n = n / 10; &nbsp; &nbsp;} &nbsp; &nbsp;return sum; } //function to count the number of pairs int paircount(int arr_1[], int arr_2[], int size1, int size2){ &nbsp; &nbsp;// set is used to avoid duplicate pairs &nbsp; &nbsp;set&lt;pair&lt;int, int&gt; &gt; myset; &nbsp; &nbsp;for (int i = 0; i &lt; size1; i++){ &nbsp; &nbsp; &nbsp; for (int j = 0; j &lt; size2; j++){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// check sum of digits &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// of both the elements &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (sumdigits(arr_1[i]) == sumdigits(arr_2[j])){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (arr_1[i] &lt; arr_2[j]){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myset.insert(make_pair(arr_1[i], arr_2[j])); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myset.insert(make_pair(arr_2[j], arr_1[i])); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;// return size of the set &nbsp; &nbsp;return myset.size(); } // Driver code int main(){ &nbsp; &nbsp;int arr_1[] = { 1, 22, 42, 17 }; &nbsp; &nbsp;int arr_2[] = { 5, 31, 6, 8 }; &nbsp; &nbsp;int size1 = sizeof(arr_1) / sizeof(arr_1[0]); &nbsp; &nbsp;int size2 = sizeof(arr_2) / sizeof(arr_2[0]); &nbsp; &nbsp;cout &lt;&lt;&quot;count is &quot;&lt;&lt;paircount(arr_1, arr_2, size1, size2); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>If we run the above code we will get the following output &amp;miuns;</p><pre class="result notranslate">count is 3</pre>
raja
Updated on 15-May-2020 11:57:58

Advertisements