human
Find the length of largest subarray with 0 sum | Returns the maximum length.
NOTE : Dictonary in python in implemented as Hash Maps Create an empty hash map ( dictionary ).
Initialize sum of elements.
Initialize result.
Traverse through the given array.
Add the current element to the sum.
NOTE : ' in ' operation in dictionary to search key takes O ( 1 ) . Look if current sum is seen before.
else put this sum in dictionary.
test array"
How can the above be solved in Python?
assistant
#Here is code to find the length of largest subarray with 0 sum and the other steps required.
 def maxLen ( arr ) :
	hash_map = { }
	curr_sum = 0
	max_len = 0
	for i in range ( len ( arr ) ) :
		curr_sum += arr [ i ]
		if arr [ i ] is 0 and max_len is 0 :
			max_len = 1
		if curr_sum is 0 :
			max_len = i + 1
		if curr_sum in hash_map :
			max_len = max ( max_len , i - hash_map [ curr_sum ] )
		else :
			hash_map [ curr_sum ] = i
	return max_len
arr = [ 15 , - 2 , 2 , - 8 , 1 , 7 , 10 , 13 ]
print " Length ▁ of ▁ the ▁ longest ▁ 0 ▁ sum ▁ subarray ▁ is ▁ % ▁ d " % maxLen ( arr )