

- 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 distinct pairs from two arrays having same sum of digits in C++
<p>We are given with two arrays let’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> − int arr_1[] = {1, 22, 42, 17} Int arr_2[] = {1, 31, 6, 8} <strong>Output</strong> − count is 4</pre><p><strong>Explanation</strong> − 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> − int arr_1[] = {1, 22, 42, 17} Int arr_2[] = {2, 78, 6, 18} <strong>Output</strong> − count is 1</pre><p><strong>Explanation</strong> − 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> − int arr_1[] = {1, 22, 42, 17} Int arr_2[] = {2, 78, 16, 18} <strong>Output</strong> − count is 0</pre><p><strong>Explanation</strong> − 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 −</h2><ul class="list"><li><p>Create two arrays let’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’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"> Live Demo</a></p><pre class="prettyprint notranslate">#include <iostream> #include <set> using namespace std; // Function to find the // sum of digits of a number int sumdigits(int n){ int sum = 0; while (n > 0){ sum += n % 10; n = n / 10; } return sum; } //function to count the number of pairs int paircount(int arr_1[], int arr_2[], int size1, int size2){ // set is used to avoid duplicate pairs set<pair<int, int> > myset; for (int i = 0; i < size1; i++){ for (int j = 0; j < size2; j++){ // check sum of digits // of both the elements if (sumdigits(arr_1[i]) == sumdigits(arr_2[j])){ if (arr_1[i] < arr_2[j]){ myset.insert(make_pair(arr_1[i], arr_2[j])); } else{ myset.insert(make_pair(arr_2[j], arr_1[i])); } } } } // return size of the set return myset.size(); } // Driver code int main(){ int arr_1[] = { 1, 22, 42, 17 }; int arr_2[] = { 5, 31, 6, 8 }; int size1 = sizeof(arr_1) / sizeof(arr_1[0]); int size2 = sizeof(arr_2) / sizeof(arr_2[0]); cout <<"count is "<<paircount(arr_1, arr_2, size1, size2); return 0; }</pre><h2>Output</h2><p>If we run the above code we will get the following output &miuns;</p><pre class="result notranslate">count is 3</pre>
- Related Questions & Answers
- Count pairs from two arrays having sum equal to K in C++
- Count Pairs from two arrays with even sum in C++
- Count pairs formed by distinct element sub-arrays in C++
- Count pairs from two arrays whose modulo operation yields K in C++
- Maximum count of pairs which generate the same sum in C++
- Count pairs from two sorted arrays whose sum is equal to a given value x in C++
- Count all sub-arrays having sum divisible by k
- Count subarrays having total distinct elements same as original array in C++
- Maximizing Unique Pairs from two arrays in C++
- Sum of special triplets having elements from 3 arrays in C++
- Count number of distinct pairs whose sum exists in the given array in C++
- Count of subsequences having maximum distinct elements in C++
- Count subsets having distinct even numbers in C++
- C++ code to count number of operations to make two arrays same
- Find Sum of pair from two arrays with maximum sum in C++
Advertisements