human
Can you provide a code to trapping Rain Water and the other steps required given the following:
Trapping Rain Water | Function to return the maximum water that can be stored.
indices to traverse the array.
To store Left max and right max for two pointers left and right.
To store the total amount of rain water trapped.
We need check for minimum of left and right max for each element.
Add the difference between current value and right max at index r.
Update right max.
Update right pointer.
Add the difference between current value and left max at index l.
Update left max.
Update left pointer.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to trapping Rain Water and the other steps required.
 def maxWater ( arr , n ) :
	left = 0
	right = n - 1
	l_max = 0
	r_max = 0
	result = 0
	while ( left <= right ) :
		if r_max <= l_max :
			result += max ( 0 , r_max - arr [ right ] )
			r_max = max ( r_max , arr [ right ] )
			right -= 1
		else :
			result += max ( 0 , l_max - arr [ left ] )
			l_max = max ( l_max , arr [ left ] )
			left += 1
	return result
arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ]
n = len ( arr )
print ( maxWater ( arr , n ) )