human
Count pairs from two arrays with difference exceeding K | Function to count pairs that satisfy the given conditions.
Stores index of the left pointer ..
Stores index of the right pointer.
Stores count of total pairs that satisfy the conditions.
Sort arr [ ] array.
Sort brr [ ] array.
Traverse both the array and count then pairs.
If the value of ( brr [ j ] - arr [ i ] ) exceeds K.
Update cntPairs.
Update.
Update j.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to count pairs from two arrays with difference exceeding K and the other steps required.
 def count_pairs ( arr , brr , N , M , K ) :
	i = 0
	j = 0
	cntPairs = 0
	arr = sorted ( arr )
	brr = sorted ( brr )
	while ( i < N and j < M ) :
		if ( brr [ j ] - arr [ i ] > K ) :
			cntPairs += ( M - j )
			i += 1
		else :
			j += 1
	return cntPairs
if __name__ == ' _ _ main _ _ ' :
	arr = [ 5 , 9 , 1 , 8 ]
	brr = [ 10 , 12 , 7 , 4 , 2 , 3 ]
	K = 3
	N = len ( arr )
	M = len ( brr )
	print ( count_pairs ( arr , brr , N , M , K ) )