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


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.

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.

For Example

Input − int arr_1[] = {1, 22, 42, 17}
   Int arr_2[] = {1, 31, 6, 8}
Output − count is 4

Explanation − in total there are 4 pairs having the same sum of digits and those are (1, 1), (22, 31), (42, 6) and (17, 8).

Input − int arr_1[] = {1, 22, 42, 17}
   Int arr_2[] = {2, 78, 6, 18}
Output − count is 1

Explanation − in total there is only one pair having the same sum of digits and that is (42, 6).

Input − int arr_1[] = {1, 22, 42, 17}
   Int arr_2[] = {2, 78, 16, 18}
Output − count is 0

Explanation − There is no pair having the same sum of digits so count is 0.

Approach used in the below program is as follows −

  • Create two arrays let’s say, arr_1[] and arr_2[]

  • Calculate the length of both the arrays using the length() function that will return an integer value as per the elements in an array.

  • Create a set type variable let’s say st

  • Start loop for i to 0 and i less than size of arr_1[]

  • Inside the loop, Start another loop for j to 0 and j less than size of arr_2[].

  • 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])

  • Else, insert(make_pair(arr_2[j], arr_1[i]).

  • Return st.size()

  • Print the result.

Example

 Live Demo

#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;
}

Output

If we run the above code we will get the following output &miuns;

count is 3

Updated on: 15-May-2020

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements