BackmediumTwo PointersFlipkart

Max Contained Volume Solution

Problem Statement

Given a list of non-negative integers heights representing the heights of containers, find the maximum volume of liquid that can be stored between any two containers, where the volume is calculated as the minimum height of the two containers multiplied by the distance between their original indices.

Example 1
Input
[1,8,6,2,5,4,8,3,7]
Output
49

Explanation: Step-by-step: with input [1,8,6,2,5,4,8,3,7], we calculate the volume between each pair of containers. The maximum volume is achieved between containers at indices 1 and 8, with heights 8 and 7 respectively. The distance between them is 8 - 1 = 7. The minimum height is min(8, 7) = 7. Therefore, the maximum volume is 7 * 7 = 49.

Example 2
Input
[4,3,2,1,4]
Output
16

Explanation: Step-by-step: with input [4,3,2,1,4], we calculate the volume between each pair of containers. The maximum volume is achieved between containers at indices 0 and 4, with heights 4 and 4 respectively. The distance between them is 4 - 0 = 4. The minimum height is min(4, 4) = 4. Therefore, the maximum volume is 4 * 4 = 16.

Constraints

  • 2 <= n <= 10^5
  • 0 <= heights[i] <= 10^4
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Max Contained Volume — Problem Statement & Solution Guide

Two PointersMediumTwo Pointers
TimeO(n^2)
|
SpaceO(1)

Problem Description

Given a list of non-negative integers heights representing the heights of containers, find the maximum volume of liquid that can be stored between any two containers, where the volume is calculated as the minimum height of the two containers multiplied by the distance between their original indices.

Examples

Example 1

Input

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

Output

49

Explanation: Step-by-step: with input [1,8,6,2,5,4,8,3,7], we calculate the volume between each pair of containers. The maximum volume is achieved between containers at indices 1 and 8, with heights 8 and 7 respectively. The distance between them is 8 - 1 = 7. The minimum height is min(8, 7) = 7. Therefore, the maximum volume is 7 * 7 = 49.

Example 2

Input

[4,3,2,1,4]

Output

16

Explanation: Step-by-step: with input [4,3,2,1,4], we calculate the volume between each pair of containers. The maximum volume is achieved between containers at indices 0 and 4, with heights 4 and 4 respectively. The distance between them is 4 - 0 = 4. The minimum height is min(4, 4) = 4. Therefore, the maximum volume is 4 * 4 = 16.

Constraints

  • 2 <= n <= 10^5
  • 0 <= heights[i] <= 10^4

Optimal Approach & Strategy

Two pointers at start and end. Calculate area (min height * width). Move the pointer pointing to the shorter line inward, as moving the taller line can never increase the area. Time O(N), Space O(1).

Brute Force Approach

Calculate area for every possible pair of lines. Time O(N^2).

Verified Code Solutions

JavaScript Solution
Time: O(n^2)
function maxArea(height) { let max = 0; let left = 0; let right = height.length - 1; while (left < right) { let h = Math.min(height[left], height[right]); let w = right - left; max = Math.max(max, h * w); if (height[left] < height[right]) { left++; } else { right--; } } return max; }

Asked in Top Tech Interviews

Flipkart

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.