ExamAdda Logo

Container With Most Water

Easy

You are given an integer array height of length n.

There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that, together with the x-axis, form a container that can hold the maximum amount of water.

Return the maximum amount of water a container can store.

You may not slant the container.

Example 1

Input

9
1 8 6 2 5 4 8 3 7

Output

49

Explanation

The maximum area is formed by the lines with heights 8 and 7.

Width = 8 - 1 = 7

Height = min(8, 7) = 7

Area = 7 × 7 = 49.

Example 2

Input

2
1 1

Output

1

Explanation

Only one container can be formed.

Width = 1

Height = min(1, 1) = 1

Area = 1.

Constraints

2 ≤ height.length ≤ 10⁵
0 ≤ height[i] ≤ 10⁴

Hints:

Hint 1

The area is determined by the shorter of the two lines and the distance between them.

Hint 2

Start with two pointers at both ends. Compute the current area and move the pointer pointing to the shorter line inward, since moving the taller line cannot increase the area while the shorter line remains the limiting factor.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 5, 2026

Last updated

Aug 6, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output