human
Find subarray with given sum | Set 2 ( Handles Negative Numbers ) | Function to print subarray with sum as given sum.
create an empty map.
if curr_sum is equal to target sum we found a subarray starting from index 0 and ending at index i.
If curr_sum - sum already exists in map we have found a subarray with target sum.
if value is not present then add to hashmap.
If we reach here , then no subarray exists.
Driver program to test above function"
How can the above be solved in Python?
 ... Write me a code to find subarray with given sum and the other steps required?
assistant
#Here is code to find subarray with given sum and the other steps required.
 def subArraySum ( arr , n , Sum ) :
	Map = { }
	curr_sum = 0
	for i in range ( 0 , n ) :
		curr_sum = curr_sum + arr [ i ]
		if curr_sum == Sum :
			print ( " Sum ▁ found ▁ between ▁ indexes ▁ 0 ▁ to " , i )
			return
		if ( curr_sum - Sum ) in Map :
			print ( " Sum ▁ found ▁ between ▁ indexes " ,   \ Map [ curr_sum - Sum ] + 1 , " to " , i )
			return
		Map [ curr_sum ] = i
	print ( " No ▁ subarray ▁ with ▁ given ▁ sum ▁ exists " )
if __name__ == " _ _ main _ _ " :
	arr = [ 10 , 2 , - 2 , - 20 , 10 ]
	n = len ( arr )
	Sum = - 10
	subArraySum ( arr , n , Sum )