Container With Most Water

Difficulty: Medium, Asked-in: Google, Facebook, Amazon, Adobe

Key takeaway: An excellent coding problem to learn problem-solving using the two-pointers approach, where both pointers are moving in the opposite direction. The idea and proof behind the efficient solution are intuitive and worth exploring. 

Let’s understand the problem

Given an array of n non-negative integers height [n], where each value represents a point at coordinate (i, height[i]). Now n vertical lines are drawn such that the two endpoints of line i are at (i, 0) and (i, height[i]). Here each pair of a line with the x-axis forms a container.

Write a program to find two lines, such that the container contains the most water. We should return an integer that corresponds to the maximum area of water that can be contained.

Problem note

  • The value of n is at least 2.
  • We need to maximise the area formed between the vertical lines using the shorter line as height and the distance between the lines as the width i.e Area = max [(j  -  i) * min (height[i], height[j])].

Example

Input: height[] = [1, 5, 6, 3, 4, 2], Output: 12.

Explanation: The area between lines 5 and 4 will be maximum. 5 and 4 are distance 3 apart, so the size of the base = 3. Height of container = min(5, 4) = 4, So total area = 3 * 4 = 12. Refer to the following image for better clarity.

understanding container with most water

Discussed solution approaches

  • Brute force approach  using nested loops
  • Efficient solution  using two pointers

Brute force approach  using nested loops

Solution idea and steps

The basic idea is to consider every possible pair of lines and find the pair that forms the maximum area. The area between each pair of lines (i, j) is (j - i) * min(height[i], height[j]) .

  • We initialise a variable, maxArea, to track the maximum area between pairs of lines.
  • We run nested loops to consider each pair of lines: the outer loop runs from i = 0 to n - 1, and the inner loop runs from j = i to n - 1.
  • For every pair of lines i and j, we calculate the area using the formula above and store it in a temporary variable, currArea. If currArea > maxArea, we update maxArea: maxArea = max(maxArea, currArea).
  • After the nested loops finish, we return the value stored in maxArea.

Solution pseudocode

maxContainerWater(height, n)
    maxArea = 0
    // Consider every pair of lines
    for i from 0 to n - 1
        for j from i to n - 1
            // Calculate the area of water held between lines i and j
            currArea = (j - i) * min(height[i], height[j])
            // Update the maximum area found so far
            maxArea = max(maxArea, currArea)

    return maxArea

Solution analysis

We are considering every possible pair of lines using two nested loops. Total no. of pairs = n(n-1)/2 (Think!). Time complexity = O(n²). Space complexity = O(1), we are using a constant number of variables.

Efficient solution  using two pointers approach

Solution idea

Now the critical questions are: how can we improve the time complexity? What kind of information do we use to optimise it further? In the above approach, we are exploring all the pairs of i and j calculating the area using the formula (j - i) * min (height[j], height[i]). So rather than choosing all pairs, can we cover all possibilities of (i, j) in a wise way and do it using a single loop? Think!

Here is a solution idea: we take two pointers, one at the beginning and one at the end of the height[] array, and maintain a variable maxArea to store the maximum area between the lines. At every step, we calculate the area formed between the two pointers, update the maxArea and move the pointer pointing to the shorter line towards the other end by one. But a critical question is: why are we doing this? Here is an explanation by considering two possible scenarios when (height[i] < height[j]) and (height[i] > height[j]).

Case 1: if (height[i] < height[j])

When height[i] < height[j], we don’t need to calculate the area for all pairs between (i, j - 1), (i, j - 2),…because these areas are smaller than the area at (i, j). Let's understand it better by comparing the area for (i, j) and (i, j - 1).

  • A = Area for pair (i, j) = (j  - i) * min (height[i], height[j]) = (j  -  i) * height[i]
  • A’ = Area for pair (i, j - 1) = (j  - 1  -  i) * min (height[i], height[j - 1])

if(height[i] < height[j - 1])

=> A’ = (j  -  1  -  i) * min (height[i], height[j - 1]) = (j - 1 - i) * height[i]

=> A' = (j - 1 - i) * height[i] < (j - i) * height[i] (As we know, A = (j - i) * height[i])

=> A’ < A

if(height[i] > height[j - 1])

=> A’ = (j  -  1  -  i) * min (height[i], height[j - 1]) = (j - 1 - i) * height[j-1]

=> A’ = (j - 1 - i) * height[j-1] < (j - i) * height[i] (As we know, A = (j - i) * height[i])

=> A' < A

So overall, when height[i] < height[j], the area between pair of lines (i, j) is less than (i, j - 1). With a similar argument, areas between the pairs (i, j - 2), (i, j - 3),…will be automatically less than the area between the pair (i, j). In simple words: we don’t need to consider the area between the pairs (i, j - 1), (i, j - 2), (i, j - 3), etc., and move the pointer i to one right in search of a larger area. Think!

Case 2: if (height[i] > height[j])

When height[i] > height[j], we don’t need to calculate all (i + 1, j), (i + 2, j),…because these areas are smaller than our area at (i, j). So we move the pointer j to one left in search of a larger area. We can use an idea similar to the above approach for the proof. (Think!)

Solution steps

  • We initialise the variable maxArea to store the maximum area.
  • We use two pointers i and j initialised at 0 and n-1, respectively. 
  • Now we run a loop till the left pointer i is less than the right pointer j.
  • We calculate the area formed between pointers i and j using the formula (j - i)*min(height[i], height[j]) and update the maxArea.
  • If (height[i] < height[j]), we move pointer i by one right. Otherwise, we move pointer j by one left.
  • By the end of the loop, our max area gets stored in the variable maxArea.

Solution pseudocode

maxContainerWater(height, n)
    maxArea = 0
    i = 0
    j = n - 1

    // Move the pointers inward to search for the maximum area
    while i < j
        currArea = (j - i) * min(height[i], height[j])
        maxArea = max(maxArea, currArea)

        // Move the pointer at the shorter line inward
        if height[i] < height[j]
            i = i + 1
        else
            j = j - 1

    return maxArea

Solution analysis

At every iteration of the while loop, we perform one comparison and move one pointer by 1. In the worst case, we are scanning the input array once using the left and right pointers. Time complexity = O(n)(Think!). Space complexity = O(1), we are using a constant number of variables.

Critical ideas to think!

  • Can we solve this problem using another approach?
  • For the area for the pair (i, j), why do we require a minimum of height[i] and height[j]?
  • Does the above solution work when values are repeated i.e. any two vertical lines have the same height? In this scenario, how do we modify the above code to get the correct output? How do we decide to move pointers? 
  • Visualise the correctness of the two-pointer approach.

Comparisons of time and space complexities

  • Using nested loops : Time = O(n^2), Space = O(1)
  • Using two pointers: Time = O(n), Space = O(1)

Similar coding questions to practice

Enjoy learning, Enjoy coding, Enjoy algorithms!

Visualization Lab

See how algorithmic problem solving approaches work and follow each step at your own pace.

Explore Visualisation Lab

Courses and Blogs