Container With Most Water
medium⏱ 15 mintypescriptchallengesproblem-solving-intermediate-
Runs in your browser
- Before starting
- Use a supported browser; initial dependencies may need internet.
- What it proves
- Run shows output; Test checks the authored cases, not every possible input.
- When finished
- Stop active runs when finished. No cloud infrastructure is provisioned by this exercise.
LEARN BY DOINGPut your intuition to work3 steps
Container With Most Water
Use the lesson’s examples and explanation to check your idea.
Personal reflection. Nothing is sent to an AI service.Before you start
Browser lesson checks; review each requirement in Tests.
Catalog level: Medium · Estimated time: 15 min. Your prior experience can change the difficulty.
Sample case — your program reads stdin and prints to stdout.
Need a hand?
Start with a small test case. Hints progress from an idea to an explanation and, when requested, code.
Starter Given n non-negative integers representing heights of vertical lines at positions 0 to n-1, find two lines that together with the x-axis form a container that holds the most water.
Use two pointers from both ends, moving the shorter one inward.
Area = min(height[left], height[right]) * (right - left)
Example: [1,8,6,2,5,4,8,3,7] → Max area = 49 (between positions 1 and 8)
Input format
Line 1: n. Line 2: n space-separated integers (heights).
Output format
The maximum area.
Constraints
2 ≤ n ≤ 10⁵, 0 ≤ height[i] ≤ 10⁴