Count pairs from two arrays whose modulo operation yields K | C ++ implementation of above approach ; Function to return the total pairs of elements whose modulo yield K ; set is used to avoid duplicate pairs ; check which element is greater and proceed according to it ; check if modulo is equal to K ; return size of the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalPairs ( int arr1 [ ] , int arr2 [ ] , int K , int n , int m ) { set < pair < int , int > > s ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr1 [ i ] > arr2 [ j ] ) { if ( arr1 [ i ] % arr2 [ j ] == K ) s . insert ( make_pair ( arr1 [ i ] , arr2 [ j ] ) ) ; } else { if ( arr2 [ j ] % arr1 [ i ] == K ) s . insert ( make_pair ( arr2 [ j ] , arr1 [ i ] ) ) ; } } } return s . size ( ) ; } int main ( ) { int arr1 [ ] = { 8 , 3 , 7 , 50 } ; int arr2 [ ] = { 5 , 1 , 10 , 4 } ; int K = 3 ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int m = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << totalPairs ( arr1 , arr2 , K , n , m ) ; return 0 ; }