Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find the maximum area formed by two lines

Given heights of vertical lines on the x-axis, find the maximum area formed between any two lines using the two-pointer technique.

Python practice15 minTwo Pointers & Sliding WindowIntermediateLast updated March 26, 2026

Problem statement

You are given a list of non-negative integers heights where each element represents the height of a vertical line at that index. A container is formed between two lines i and j (i < j) and the area it can contain is min(heights[i], heights[j]) * (j - i). Return the maximum area that can be formed by any two lines. You should implement an algorithm with O(n) time complexity and O(1) additional space using the two-pointer approach.

Task

Implement an efficient two-pointer solution that computes the maximum water container area in O(n) time and O(1) extra space.

Examples

Classic example

Input

heights = [1, 8, 6, 2, 5, 4, 8, 3, 7]

Output

49

Choose lines at indices 1 (height 8) and 8 (height 7): area = min(8,7) * (8-1) = 7 * 7 = 49.

Input format

A single list of integers representing heights: heights

Output format

An integer representing the maximum possible area.

Constraints

2 <= len(heights) <= 10^5; 0 <= heights[i] <= 10^4. Expected time: O(n), extra space: O(1).

Samples

Sample 1

Input

[1, 1]

Output

1

Only two lines with height 1 and distance 1 => area = 1.