human
Can you provide a code to count pairs from two arrays whose modulo operation yields K and the other steps required given the following:
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"
How can the above be solved in Python?
assistant
#Here is code to count pairs from two arrays whose modulo operation yields K and the other steps required.
 def totalPairs ( arr1 , arr2 , K , n , m ) :
	s = { }
	for i in range ( n ) :
		for j in range ( m ) :
			if ( arr1 [ i ] > arr2 [ j ] ) :
				if ( arr1 [ i ] % arr2 [ j ] == K ) :
					s [ ( arr1 [ i ] , arr2 [ j ] ) ] = 1
			else :
				if ( arr2 [ j ] % arr1 [ i ] == K ) :
					s [ ( arr2 [ j ] , arr1 [ i ] ) ] = 1
	return len ( s )
arr1 = [ 8 , 3 , 7 , 50 ]
arr2 = [ 5 , 1 , 10 , 4 ]
K = 3
n = len ( arr1 )
m = len ( arr2 )
print ( totalPairs ( arr1 , arr2 , K , n , m ) )