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
def countPairs ( A1 , A2 , n1 , n2 , K ) : NEW_LINE INDENT res = 0 NEW_LINE m = dict ( ) NEW_LINE for i in range ( 0 , n1 ) : NEW_LINE INDENT if A1 [ i ] not in m . keys ( ) : NEW_LINE INDENT m [ A1 [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ A1 [ i ] ] = m [ A1 [ i ] ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n2 ) : NEW_LINE INDENT temp = K - A2 [ i ] NEW_LINE if temp in m . keys ( ) : NEW_LINE INDENT res = res + 1 NEW_LINE m [ temp ] = m [ temp ] - 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT A1 = [ 1 , 1 , 3 , 4 , 5 , 6 , 6 ] NEW_LINE A2 = [ 1 , 4 , 4 , 5 , 7 ] NEW_LINE K = 10 NEW_LINE n1 = len ( A1 ) NEW_LINE n2 = len ( A2 ) NEW_LINE print ( countPairs ( A1 , A2 , n1 , n2 , K ) ) NEW_LINE