human
Write me a code to longest Span with same Sum in two Binary arrays and the other steps required given the following:
Longest Span with same Sum in two Binary arrays | Returns largest common subarray with equal number of 0 s and 1 s.
Find difference between the two.
Creates an empty hashMap hM.
Initialize sum of elements.
Initialize result.
Traverse through the given array.
Add current element to sum.
To handle sum = 0 at last index.
If this sum is seen before , then update max_len if required.
Else put this sum in hash table.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to longest Span with same Sum in two Binary arrays and the other steps required.
 def longestCommonSum ( arr1 , arr2 , n ) :
	arr = [ 0 for i in range ( n ) ]
	for i in range ( n ) :
		arr [ i ] = arr1 [ i ] - arr2 [ i ] ;
	hm = { }
	sum = 0
	max_len = 0
	for i in range ( n ) :
		sum += arr [ i ]
		if ( sum == 0 ) :
			max_len = i + 1
		if sum in hm :
			max_len = max ( max_len , i - hm [ sum ] )
		else :
			hm [ sum ] = i
	return max_len
arr1 = [ 0 , 1 , 0 , 1 , 1 , 1 , 1 ]
arr2 = [ 1 , 1 , 1 , 1 , 1 , 0 , 1 ]
n = len ( arr1 )
print ( longestCommonSum ( arr1 , arr2 , n ) )