#K63357. Maximum Water Trapped Between Poles
Maximum Water Trapped Between Poles
Maximum Water Trapped Between Poles
You are given an array of n integers representing the heights of poles arranged in a line. Your task is to find two poles such that the container they form could trap the maximum amount of water. The amount of water trapped between two poles at positions i and j is given by the formula:
$$W = \min(\text{height}_i,\ \text{height}_j) \times (j - i)$$
Compute and output the maximum water that can be trapped between any two poles. This is a classic two-pointer problem and requires an efficient solution.
Example:
Input: 6 1 8 6 2 5 4 Output: 16
inputFormat
The first line of input contains a single integer n, the number of poles.
The second line contains n space-separated integers representing the heights of the poles.
outputFormat
Output a single integer representing the maximum water that can be trapped between any two poles.
## sample6
1 8 6 2 5 4
16