intermediate · two pointers · greedy · arrays
Two walls that hold the most water
Problem
Wall heights stand at positions 0, 1, …, n − 1 along a line. Two walls and the ground between them form a container whose water level is the shorter wall. Find the largest amount of water (width × height) two walls can hold.
Constraints
- 2 ≤ n ≤ 32
- heights from 0 to 999
Examples
[1, 8, 6, 2, 5, 4, 8, 3, 7] → 49 — Walls at 1 and 8: width 7, height 7.
Show the solution
Two pointers, moving the shorter wall
The area is limited by the shorter wall. Any container using the shorter wall with a closer partner is narrower and no taller, so the shorter wall can be dropped.
Why it is correct
Each step discards a wall that cannot be part of a better container than ones already measured, so the best container is never discarded before it is measured.
Complexity
Time O(n), extra space O(1). The pointers move towards each other one step per iteration: n − 1 iterations.
For contrast: Measure every pair. That costs O(n²) time and O(1) space.
Edge cases
- Equal heights: moving either wall is safe.
- Zero-height walls hold nothing.
Common mistakes
- Moving the taller wall.
- Using the taller wall as the water level.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class ContainerArea
{
/// <summary>The most water two walls can hold: max over i < j of (j - i) * min(h[i], h[j]). O(n).</summary>
public static int Max(int[] heights)
{
int left = 0, right = heights.Length - 1, best = 0;
while (left < right)
{
int area = (right - left) * Math.Min(heights[left], heights[right]);
best = Math.Max(best, area);
if (heights[left] < heights[right])
left++; // the shorter wall cannot do better with a closer partner
else
right--;
}
return best;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/ContainerArea.cs
from collections.abc import Sequence
def max_area(heights: Sequence[int]) -> int:
"""The most water two walls can hold: max over i < j of (j - i) * min(h[i], h[j]). O(n)."""
left, right, best = 0, len(heights) - 1, 0
while left < right:
area = (right - left) * min(heights[left], heights[right])
best = max(best, area)
if heights[left] < heights[right]:
left += 1 # the shorter wall cannot do better with a closer partner
else:
right -= 1
return best
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/container_area.py
package dev.bekadan.dsa.problems;
public final class ContainerArea {
private ContainerArea() {
}
/** The most water two walls can hold: max over i < j of (j - i) * min(h[i], h[j]). O(n). */
public static int max(int[] heights) {
int left = 0, right = heights.length - 1, best = 0;
while (left < right) {
int area = (right - left) * Math.min(heights[left], heights[right]);
best = Math.max(best, area);
if (heights[left] < heights[right]) {
left++; // the shorter wall cannot do better with a closer partner
} else {
right--;
}
}
return best;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/ContainerArea.java
#pragma once
#include <algorithm>
#include <cstddef>
#include <span>
namespace bekadan::dsa::problems {
// The most water two walls can hold: max over i < j of (j - i) * min(h[i], h[j]). Precondition: size() >= 2. O(n).
inline int container_area(std::span<const int> heights) {
std::size_t left = 0, right = heights.size() - 1;
int best = 0;
while (left < right) {
const int area = static_cast<int>(right - left) * std::min(heights[left], heights[right]);
best = std::max(best, area);
if (heights[left] < heights[right])
++left; // the shorter wall cannot do better with a closer partner
else
--right;
}
return best;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/container_area.hpp