You are given a list of non-negative integers where each element represents the height of a vertical line drawn at that index. Find two lines that, together with the x-axis, form a container that holds the most water.
The area of water between lines at index i and index j (where i < j) is calculated as: min(height[i], height[j]) * (j - i).
Return the maximum area of water that can be contained. Return 0 if the list is null or has fewer than 2 elements.
Examples:
Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49
Explanation: Lines at index 1 (height 8) and index 8 (height 7) form a container with area min(8, 7) * (8 - 1) = 7 * 7 = 49.
Input: height = [1, 1]
Output: 1
Explanation: The only pair is index 0 and index 1. Area = min(1, 1) * (1 - 0) = 1.
Input: height = [4, 3, 2, 1, 4]
Output: 16
Explanation: Lines at index 0 (height 4) and index 4 (height 4) form a container with area min(4, 4) * (4 - 0) = 4 * 4 = 16.
Input: height = [1, 2, 1]
Output: 2
Explanation: Best area is min(1, 1) * (2 - 0) = 1 * 2 = 2.
Input: height = []
Output: 0
Explanation: Fewer than 2 elements returns 0.
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay September 2026 (Sep 1–15)Sponsored by FLXBL
Contest runs September 1 - 15. Complete challenges to climb the leaderboard!
1st & 2nd: Salesforce certification exam voucher — see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.