Container with most water
Is it possible to solve it mathematically and make it faster?


Not faster than the second solution,
which is \$O(n)\$, making a single pass over the input.
It's easy to see that it's not possible to find the correct answer without inspecting every element. For example, if two values are not inspected, then one could pick arbitrarily high values for them to make them produce the maximum area.

I have some style comments about the second solution:


I dislike the variable name l because in some fonts it can be confused with | or 1.
I would rename l to left and r to right for natural clarity. Still simple enough.
For symmetry with the while (l &lt; r), I would rewrite the if in the loop body to be if (height[l] &lt; height[r]), as I feel it would facilitate understanding of the logic.