Count pairs from two arrays whose modulo operation yields K in C++

C++Server Side ProgrammingProgramming

We are given two arrays containing positive numbers and a value K. The goal is to find unique pairs of elements of arrays such that pairs of type (A,B) has A%B=K or B%A=K and A belongs to the first array and B belongs to the second array.

Let us understand with examples

Input − arr_1[] = {1,2,5,3,4}; arr_2[] = {7,1,3}; k=2

Output − Count of pairs from two arrays whose modulo operation yields K are − 2

Explanation − The pairs are (5,7) - (arr_1[2],arr_2[1]) 7%5=2 and (5,3) - (arr_1[2],arr_2[2]) 5%3=2

Input − arr_1[] = {2,5}; arr_2[] = {3,7}; k=1

Output − Count of pairs from two arrays whose modulo operation yields K are − 2

Explanation − The pairs are (2,3) - (arr_1[0],arr_2[0]) 3%2=1 and (2,7) - (arr_1[0],arr_2[1]) 7%2=1

Approach used in the below program is as follows

In this approach we will traverse both arrays using for loop. Insert pairs to set<pair<int, int> > se in which A%B=k or B%A=k, where A belongs to arr_1 and B belongs to arr_2. At the end size of set se is the number of unique pairs from two arrays whose modulo operation yields k.

  • Take integer arrays arr_1[] and arr_2[] with positive elements and lengths as size_arr_1 and size_arr_2.

  • Take integers k.

  • Function modulo_pairs(int arr_1[], int arr_2[], int size_arr_1, int size_arr_2, int k) takes both arrays and their lengths and returns the pairs such that the modulo operation of elements of both arrays yields k.

  • Take the initial value of count as 0.

  • Take set<pair<int, int> > se; of pairs <int,int>.

  • Start traversing arr_1[] from i=0 to i<size_arr_1 and arr_2[] from j=0 to j<size_arr_2.

  • For each pair arr_1[i], arr_2[j], check if arr_1[i]>arr_2[j]. If yes check if arr_1[i]%arr_2[j]==k. If true then make a pair of arr_1[i] and arr_2[j] and insert to set se.

  • Else check if arr_2[j]%arr_1[i]==k. If true then make a pair of arr_1[i] and arr_2[j] and insert to set se.

  • Calculate count as se.size(). For count of unique pairs.

  • Return count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int modulo_pairs(int arr_1[], int arr_2[], int size_arr_1, int size_arr_2, int k){
   int count = 0;
   set<pair<int, int> > se;
   for (int i = 0; i < size_arr_2; i++){
      for (int j = 0; j < size_arr_1; j++){
         if (arr_1[i] > arr_2[j]){
            if (arr_1[i] % arr_2[j] == k){
               se.insert(make_pair(arr_1[i], arr_2[j]));
            }
         }
         else{
            if (arr_2[j] % arr_1[i] == k){
               se.insert(make_pair(arr_2[j], arr_1[i]));
            }
         }
      }
   }
   count = se.size();
   return count;
}
int main(){
   int arr_1[] = { 2, 7, 1, 9 };
   int arr_2[] = { 4, 10, 3, 10 };
   int size_arr_1 = sizeof(arr_1) / sizeof(arr_1[0]);
   int size_arr_2 = sizeof(arr_2) / sizeof(arr_2[0]);
   int k = 3;
   cout<<"Count of pairs from two arrays whose modulo operation yields K are:"<<modulo_pairs(arr_1, arr_2, size_arr_1, size_arr_2, k);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of pairs from two arrays whose modulo operation yields K are: 2
raja
Published on 02-Dec-2020 11:51:31
Advertisements