human
Print all the pairs that contains the positive and negative values of an element | Utility binary search.
Function.
Sort the array.
Traverse the array.
For every arr [ i ] < 0 element , do a binary search for arr [ i ] > 0..
If found , print the pair ..
Driver code"
How can the above be solved in Python?
 Can you provide a code to print all the pairs that contains the positive and negative values of an element and the other steps required?
assistant
#Here is code to print all the pairs that contains the positive and negative values of an element and the other steps required.
 def binary_search ( a , x , lo = 0 , hi = None ) :
	if hi is None :
		hi = len ( a )
	while lo < hi :
		mid = ( lo + hi ) // 2
		midval = a [ mid ]
		if midval < x :
			lo = mid + 1
		elif midval > x :
			hi = mid
		else :
			return mid
	return - 1
def printPairs ( arr , n ) :
	pair_exists = Fals
	arr . sort ( )
	for i in range ( n ) :
		if ( arr [ i ] < 0 ) :
			if ( binary_search ( arr , - arr [ i ] ) ) :
				print ( arr [ i ] , " , ▁ " , - arr [ i ] )
				pair_exists = True
		else :
			break
	if ( pair_exists == False ) :
		print ( " No ▁ such ▁ pair ▁ exists " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ]
	n = len ( arr )
	printPairs ( arr , n )