Count pairs from two arrays whose modulo operation yields K | 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
function totalPairs ( arr1 , arr2 , K , n , m ) { var s = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < m ; j ++ ) { if ( arr1 [ i ] > arr2 [ j ] ) { if ( arr1 [ i ] % arr2 [ j ] == K ) s . add ( [ arr1 [ i ] , arr2 [ j ] ] ) ; } else { if ( arr2 [ j ] % arr1 [ i ] == K ) s . add ( [ arr2 [ j ] , arr1 [ i ] ] ) ; } } } return s . size ; } var arr1 = [ 8 , 3 , 7 , 50 ] ; var arr2 = [ 5 , 1 , 10 , 4 ] ; var K = 3 ; var n = arr1 . length ; var m = arr2 . length ; document . write ( totalPairs ( arr1 , arr2 , K , n , m ) ) ;