#K38127. Container With Most Water

    ID: 26130 Type: Default 1000ms 256MiB

Container With Most Water

Container With Most Water

Given an array of non-negative integers representing the heights of buildings aligned in a row, determine the maximum amount of water that can be trapped between two buildings. The container is formed by choosing two buildings and using the x-axis as the base. The water contained is limited by the shorter of the two buildings and the width between them.

The formula to calculate the area of water between two buildings at indices i and j is given by:

$$Area = \min(\text{height}[i],\,\text{height}[j]) \times (j-i)$$

Use an efficient algorithm (such as the two-pointer approach) to solve the problem.

inputFormat

The first line contains an integer n representing the number of buildings. The second line contains n space-separated integers representing the heights of the buildings.

outputFormat

Output a single integer representing the maximum water that can be trapped.

## sample
9
1 8 6 2 5 4 8 3 7
49