A workshop has n vertical planks in a row, the i-th with non-negative height h[i]; consecutive planks are spaced one unit apart. Choosing two planks i < j, you can form a water tank whose two sides are those planks; the tank holds water up to the shorter of the two chosen planks, so the amount it holds equals min(h[i], h[j]) * (j - i) (the horizontal distance times the limiting height). Report the maximum amount any pair of planks can hold.
Line 1: an integer n.
Line 2: n space-separated non-negative integers, the plank heights left to right.
A single integer: the maximum water area achievable by any pair of planks.
Example 1
Input
6 1 8 6 2 5 4
Expected
16
Explanation
The largest area is 16, formed by the plank of height 8 and the plank of height 4 that sit four positions apart: min(8, 4) * 4 = 16.
Example 2
Input
2 4 9
Expected
4
Explanation
With only two planks the tank holds min(4,9) * (1) = 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →