human
Count pairs from two arrays having sum equal to K | Function to return the count of pairs having sum equal to K.
Initialize pairs to 0.
Create dictionary of elements of array A1.
count total pairs.
Every element can be part of at most one pair.
return total pairs.
Driver Code.
function call to print required answer"
How can the above be solved in Python?
assistant
#Here is code to count pairs from two arrays having sum equal to K and the other steps required.
 def countPairs ( A1 , A2 , n1 , n2 , K ) :
	res = 0
	m = dict ( )
	for i in range ( 0 , n1 ) :
		if A1 [ i ] not in m . keys ( ) :
			m [ A1 [ i ] ] = 1
		else :
			m [ A1 [ i ] ] = m [ A1 [ i ] ] + 1
	for i in range ( 0 , n2 ) :
		temp = K - A2 [ i ]
		if temp in m . keys ( ) :
			res = res + 1
			m [ temp ] = m [ temp ] - 1
	return res
A1 = [ 1 , 1 , 3 , 4 , 5 , 6 , 6 ]
A2 = [ 1 , 4 , 4 , 5 , 7 ]
K = 10
n1 = len ( A1 )
n2 = len ( A2 )
print ( countPairs ( A1 , A2 , n1 , n2 , K ) )