Maximum water that can be stored between two buildings | Return the maximum water that can be stored ; To store the maximum water so far ; Both the pointers are pointing at the first and the last buildings respectively ; While the water can be stored between the currently chosen buildings ; Update maximum water so far and increment i ; Update maximum water so far and decrement j ; Any of the pointers can be updated ( or both ) ; Driver code
def maxWater ( height , n ) : NEW_LINE INDENT maximum = 0 ; NEW_LINE i = 0 NEW_LINE j = n - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( height [ i ] < height [ j ] ) : NEW_LINE INDENT maximum = max ( maximum , ( j - i - 1 ) * height [ i ] ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT elif ( height [ j ] < height [ i ] ) : NEW_LINE INDENT maximum = max ( maximum , ( j - i - 1 ) * height [ j ] ) ; NEW_LINE j -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT maximum = max ( maximum , ( j - i - 1 ) * height [ i ] ) ; NEW_LINE i += 1 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT DEDENT return maximum ; NEW_LINE DEDENT height = [ 2 , 1 , 3 , 4 , 6 , 5 ] NEW_LINE n = len ( height ) NEW_LINE print ( maxWater ( height , n ) ) ; NEW_LINE