Trapping Rain Water | Function to return the maximum water that can be stored ; To store the maximum water that can be stored ; For every element of the array ; Find the maximum element on its left ; Find the maximum element on its right ; Update the maximum water ; Driver code
def maxWater ( arr , n ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT left = arr [ i ] ; NEW_LINE for j in range ( i ) : NEW_LINE INDENT left = max ( left , arr [ j ] ) ; NEW_LINE DEDENT right = arr [ i ] ; NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT right = max ( right , arr [ j ] ) ; NEW_LINE DEDENT res = res + ( min ( left , right ) - arr [ i ] ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxWater ( arr , n ) ) ; NEW_LINE DEDENT