DEV Community

Nithya Dharshini official
Nithya Dharshini official

Posted on

Container With Most Water — Beginner Two Pointer Breakdown (That “Ohhh” Moment)

When I first saw Container With Most Water, I froze.

The problem looked simple.
The brute force was easy.
But the optimized solution?

After seeing it, I had only one reaction:

“Ohhh… so THAT’S why the pointer moves.”

If you’re struggling with this problem — you’re in the right place.

🧩 Problem: Container With Most Water

Description

You are given an array where each element represents the height of a vertical line.

Choose two lines such that they form a container holding the maximum amount of water.

Example

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

Enter fullscreen mode Exit fullscreen mode

🧠 First Thought (Every Beginner’s Brain)

👉 “Check every pair and calculate area.”

That works… but it’s O(n²).

Interviewers expect O(n).

So we need a smarter approach.

🔍 Key Observation (THIS is the breakthrough)

The area depends on two things:

Area = min(height[left], height[right]) × (right - left)

Enter fullscreen mode Exit fullscreen mode

So:

Width → distance between pointers

Height → shorter line

👉 The shorter line limits the water.

This one sentence changes everything.

🚨 Why Brute Force Fails

Brute force checks all pairs:

(0,1), (0,2), (0,3)...

But most of these are useless, because:

Width keeps decreasing

Height doesn’t improve

We need to discard bad pairs early.

✍️ Simple Two Pointer Approach (Beginner English)

Step 1

Start with two pointers:

  • left at start
  • right at end

Why?
👉 Maximum width at the beginning.

Step 2

Calculate current area

area = min(height[left], height[right]) × (right - left)

Enter fullscreen mode Exit fullscreen mode

Store the maximum.

Step 3 (THE MOST IMPORTANT STEP 🔥)

Move only the shorter height pointer.

Why?

Because:

The taller line is already good
The shorter line is the bottleneck
Moving the taller one can NEVER increase area

This is the “ohhh” moment 😮

Step 4

Repeat until left < right

🔄 Pointer Movement Example (Visual Thinking)

Heights: [1,8,6,2,5,4,8,3,7]
           L               R
Enter fullscreen mode Exit fullscreen mode

min(1,7) × width → small
Move L (shorter)

           8               7
           L               R

Enter fullscreen mode Exit fullscreen mode

min(8,7) × width → better
Move R (shorter)

Repeat...
Enter fullscreen mode Exit fullscreen mode

Every move is logical, not guesswork.

💻 Convert Simple Approach to Code (C++)

#include <iostream>
#include <vector>
using namespace std;

int maxArea(vector<int>& height) {
    int left = 0;
    int right = height.size() - 1;
    int maxWater = 0;

    while (left < right) {
        int width = right - left;
        int h = min(height[left], height[right]);
        int area = width * h;

        maxWater = max(maxWater, area);

        // Move the shorter pointer
        if (height[left] < height[right])
            left++;
        else
            right--;
    }

    return maxWater;
}

int main() {
    vector<int> height = {1,8,6,2,5,4,8,3,7};
    cout << maxArea(height);
}

Enter fullscreen mode Exit fullscreen mode

⏱️ Complexity

Time: O(n)
Space: O(1)

Enter fullscreen mode Exit fullscreen mode

❌ Common Beginner Mistakes

❌ Moving both pointers
❌ Moving the taller pointer
❌ Thinking height increase > width decrease
❌ Trying sliding window here (wrong pattern)

🎯 Final Mental Rule (Memorize This)

Water is always limited by the shorter line.
So move the pointer with the smaller height.

This rule alone solves the entire problem.

Top comments (0)