New update is available. Click here to update.

Last Updated: 16 Nov, 2020

Difficulty: Moderate

```
1. You can not slant the container i.e. the height of the water is equal to the minimum height of the two lines which define the container.
2. Do not print anything, you just need to return the area of the container with maximum water.
```

```
For the above Diagram, the first red marked line is formed between coordinates (2,0) and (2,10), and the second red-marked line is formed between coordinates (5,0) and (5,9). The area of water contained between these two lines is (height* width) = (5-2)* 9 = 27, which is the maximum area contained between any two lines present on the plane. So in this case, we will return 3* 9=27.
```

```
The first line of input contains an integer â€˜Tâ€™ denoting the number of test cases.
The next â€˜2*Tâ€™ lines represent the â€˜Tâ€™ test cases.
The first line of each test case contains the number of elements in the sequence.
The second line of each test case contains â€˜nâ€™ space-separated integers which is the given sequence.
```

```
For every test case return the area of the container which can hold the maximum amount of water using any pair of lines from the given sequence.
```

```
1 <= T <= 50
0 <= N <= 10^4
1 <= A[i] <= 10^5
Time Limit: 1 sec
```

Since we need to find the container with most water, let us try to find all possible containers and choose the one which has the maximum area.

So how can we find the area of all possible containers?

We can, for each line with the position â€˜iâ€™ find another line â€˜jâ€™ such that â€˜jâ€™ > â€˜iâ€™ and find the amount of water contained i.e (â€˜jâ€™-â€™iâ€™)*min('A[i]', â€˜A[j]â€™) where â€˜A[i]â€™ and â€˜A[j]â€™ represents the height of the lines at index â€˜iâ€™ and â€˜jâ€™ respectively.

We can do this in the following way:

- Take a variable â€˜GLOBAL_MAX' to store the overall maximum area and initialize it to some very small value (eg -1).
- Let â€˜Nâ€™ be the number of lines in the sequence.

- Let â€˜HEIGHT[i]â€™ be the height of the first line of any container. Then for each â€˜iâ€™ we find â€˜HEIGHT[j]â€™ where â€˜iâ€™<j<â€™N-1â€™ which is the height of the second line for the container. We compute its current area in a variable â€˜CURRENT_AREAâ€™ which is (j-i)*min(A[j], A[i]) and compare with the maximum area i.e â€˜GLOBAL_MAXâ€™ achieved till now. If the â€˜CURRENT_AREAâ€™ is greater than â€˜GLOBAL_MAXâ€™ we update the maximum area.
- In the end, we return the value of â€˜GLOBAL_MAXâ€™.

- We know that area=width*length. So increasing the length of line or width between two lines will increase the area.
- If we start with the two extremes of the sequence, i.e the first and last line, we can achieve maximum width.
- But this does not ensure maximum area as it is possible that some pair of lines with a lesser width and much greater length than the extreme lengths can have a greater area.
- So this is obvious that if we decrease the width we need to find such a pair of lines that the length of the shorter line is more than the maximum of the previous pair of lines.
- Keeping all that in mind we can use the following strategy to find the maximum area:
- Let the â€˜i-thâ€™ line be used to denote the first line of the container and â€˜j-thâ€™ line be used to denote the second line and â€˜Nâ€™ be the number of lines in the sequence.
- We start from the widest container i.e â€˜i=1â€™ and â€˜j=Nâ€™. Now for a better answer with a lesser width, it is necessary that the height is higher than the current heights.
- Now discard the height which is lower among the two heights â€˜iâ€™ and â€˜jâ€™ because having a greater height will allow us to have a better answer. If it is â€˜iâ€™ move the â€˜iâ€™ pointer to the right and if it is â€˜jâ€™ move it to the left.
- Calculate the area of the new container formed and update the maximum area if the newly found area is greater than the previous maximum area.
- Do this until â€˜iâ€™ is less than â€˜jâ€™.
- In the end, we return the maximum area found at the end of the loop.

SIMILAR PROBLEMS

Missing Number

Posted: 30 Oct, 2022

Difficulty: Easy

Longest Subarray With Zero Sum

Posted: 3 Nov, 2022

Difficulty: Moderate

Merge Two Sorted Arrays Without Extra Space

Posted: 19 Nov, 2022

Difficulty: Moderate

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Negative To The End

Posted: 16 Dec, 2022

Difficulty: Easy

Popular Interview Problems: