# Container With Most Water 0(n) LeetCode#11

“The container with the most water” is a LeetCode favorite. Your job is to write a function that takes in an array of numbers and returns the max area of water that could fill a container defined by the “heights” which represent the height of walls; if you're not following, that is OK! We will get to it :)

This problem is moderately challenging if you have a history with math, at least is was moderately challenging for me. I passed 80% of test cases before needing to look for what I was missing.

PRO TIP — Don’t spend more than 30min on choking points. This is an unnecessary waste of time and probably won't yield positive results. And, even if you eventually solve the problem, you just wasted hrs of meaningful time “solving” when you could have spent time “understanding”.

The graph represents an array of walls with water filled to the maximum space allowed/ possible.

So, looking at this we can understand that we will want to figure out the maximum area between all heights in our graph.

We will need to keep track of the Max Area and the edges of our “Water Tank”.

So Let’s jump into the deep-end and solve this problem (outfield pun totally intended).

`function maxAreaOfWater(heights){     let maxArea=0;     let left = 0;      let right = heights.length -1;     while(left < right){     maxArea = Math.max(maxArea, Math.min(height[left],              height[right])* (right - left ));     if(height[left] < height[right]){               left++;          }else{               right--;         }     }   return maxArea;}`

Ok, so I wrote a bunch of code, but what the hell does it have to do with anything? what does it mean?

Well, I’m about to explain it now.

Ok, so we enter out function and immediately instantiate some variables, we have a maxArea that will keep track of our maxArea… we have a left and a right that will be pointers used to determine different areas within our container.

We then create a while loop, we will loop through until the left index is greater than the right index; are left and right limits will shift left and right until the greatest areas are found based… the best way to envision this is to look at our graph above and imagine the pointers moving.

our maxArea is being set by comparing the current maxArea with the LOWEST VALUE because the is the max level of water possible, multiplied by the space between the right (Greater Value) and left (Lesser Value). This will return the Max Value and set it to maxArea.

We repeat this process until all the left pointer is forced to pass the right pointer due to no other options. Note: if sides are equal, the left pointer is incremented.

I hope this cleared up any issues you may have had with this problem... the logic behind the max value is a little “Mind-Bending”. Give it time, Repetition is the law of learning.