#K70607. Container With Most Water

    ID: 33346 Type: Default 1000ms 256MiB

Container With Most Water

Container With Most Water

You are given a list of n non-negative integers where each integer represents the height of a vertical line drawn at that index on the x-axis. The task is to find two lines, which together with the x-axis, form a container that holds the maximum amount of water.

The area of water contained between two lines at positions i and j (with i < j) is determined by:

$$Area = (j - i) \times \min(heights[i],\; heights[j]) $$

Implement an efficient algorithm (preferably with a two-pointer approach) to compute the maximum area possible.

inputFormat

The input is provided via standard input (stdin) and has the following format:

  • The first line contains a single integer N representing the number of vertical lines.
  • The second line contains N space-separated integers representing the heights of these lines.

outputFormat

The output is a single integer printed to standard output (stdout) representing the maximum area of water that can be contained.

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