Given n non-negative integers where each represents a point at coordinate . ‘ n ‘ vertical lines are drawn such that the two endpoints of line i is at and .
Find two lines, which together with x-axis forms a container, such that the container contains the most water.
The program should return an integer which corresponds to the maximum area of water that can be contained ( maximum area instead of maximum volume sounds weird but this is 2D plane we are working with for simplicity ).
Note : You may not slant the container.
Input : [1, 5, 4, 3] Output : 6 Explanation : 5 and 3 are distance 2 apart. So the size of the base = 2. Height of container = min(5, 3) = 3. So total area = 3 * 2 = 6 Input : [3, 1, 2, 4, 5] Output : 12 Explanation : 5 and 3 are distance 4 apart. So the size of the base = 4. Height of container = min(5, 3) = 3. So total area = 4 * 3 = 12
Note 1 : When you consider a1 and aN, then the area is (N-1) * min(a1, aN). Note 2 : The base (N-1) is the maximum possible.
- This implies that if there was a better solution possible, it will definitely have the Height greater than min(a1, aN).
- We know that, Base min(a1, aN)
This means that we can discard min(a1, aN) from our set and look to solve this problem again from the start.
- If a1 < aN, then the problem reduces to solving the same thing for a2, aN.
- Else, it reduces to solving the same thing for a1, aN-1
- Program to check if water tank overflows when n solid balls are dipped in the water tank
- Measuring 6L water from 4L and 9L buckets
- Water drop problem
- Water Connection Problem
- Puzzle | Water Jug Problem
- Puzzle | Growing Water Lilly
- Puzzle | Cylindrical Glass and Water Measurement
- Program to find amount of water in a given glass
- Measure one litre using two vessels and infinite water supply
- Find amount of water wasted after filling the tank
- Maximum sum subarray of even length
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Calculate the number of set bits for every number from 0 to N
- Check whether a node is leaf node or not for multiple queries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar