The problem of finding the maximum water that can be contained between vertical lines in an array involves identifying two lines that, together with the x-axis, form the container with the greatest area. A naive approach iterates over all pairs of lines, calculating the water area using the formula min(arr[i], arr[j]) * (j - i). While simple, this approach has a time complexity of O(n^2), making it less efficient for large arrays.
An optimized solution uses the two-pointer technique to achieve O(n) time complexity. By initializing pointers at the start and end of the array, the method calculates the water area and adjusts the pointers based on the shorter line. This ensures the maximum area is computed efficiently by dynamically narrowing the width while seeking taller boundaries, leading to an optimal solution with constant space usage.
For more details, please go through - Container with Most Water