Container with Maximum Water

Easy
0/40
Average time to solve is 10m
profile
Contributed by
73 upvotes
Asked in companies
SalesforceMicrosoftAmazon

Problem statement

You have been given an array/list ‘ARR‘ of length ‘N’ consisting of non-negative integers ARR1, ARR2, ..., ARRN. The i’th integer denotes a point with coordinates (i, ARR[i]). ‘N’ vertical lines are drawn such that the two endpoints of the i’th line are at (i, arr[i]) and (i,0).

Your task is to find two lines, which, together with the x-axis, form a container, such that the container contains the most water. Return the maximum area of the container.

Note:
1. Consider the container to be 2-dimensional for simplicity. 
2. For any pair of sides ignore all the lines that lie inside that pair of sides. 
3. You are not allowed to slant the container.

Example

Example:

Consider 'ARR'= {1,8,6,2,5,4,8,3,7} then the vertical lines are represented by the above image. The blue section denotes the maximum water than a container can contain.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer ‘T’ denoting the number of test cases.

The first line of each test case contains a single integer ‘N’ denoting the number of elements in the array/list.

The second line of each test case contains ‘N’ single space-separated integers denoting the elements of the array/list.
Output Format:
For each test case, return a single integer which denotes the maximum area of the container.
Note:
You don’t need to print the output, It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
2 <= N <= 5000
1 <= ARR[i] <= 10^5

Where 'ARR[i]' denotes the elements of the given array/list.

Time limit: 1 sec
Sample Input 1 :
2
5
2 4 7 1 3
3
7 5 9
Sample Output 1:
9
14
Explanation of Sample Input 1:

Example

In the first test case, we will get the maximum area if we choose the container coloured blue in the above image. The length of the base of the container is 3, and the height of the container is min(4, 3) which is 3. Thus, the area of the container is 3 * 3 = 9.

Example

In the second test case, we will get the maximum area if we choose the container coloured blue in the above image. The length of the base of the container is 2, and the height of the container is min(7, 9) which is 7. Thus, the area of the container is 2 * 7 = 14.
Sample Input 2:
2
5
1 5 12 2 1
5
7 12 9 20 8
Sample Output 2:
5
28
Explanation of Sample Input 2:

Example

 In the first test case, we will get the maximum area if we choose the container coloured blue in the above image. The length of the base of the container is 1, and the height of the container is min(5, 12) which is 5. Thus, the area of the container is 1 * 5 = 5.

Example

In the second test case, we will get the maximum area if we choose the container coloured blue in the above image. The length of the base of the container is 4, and the height of the container is min(7, 8) which is 7. Thus, the area of the container is 4 * 7 = 28.
Hint

Try to select all possible pairs as sides of the container.

Approaches (2)
Brute Force

The idea is to try all possible pairs as the sides of the container and then calculate the maximum area out of all the pairs.

 

The steps are as follows:

 

  1. We will iterate through the array/list and pivot each element as the left side of the container. Let’s say this element is at the 'i’th index.
  2. And for the right side of the container start exploring from the next index of ‘i’ and pivot each element as the right side of the container. Let’s call this index ‘j’.
  3. The area of the container with the sides as ‘ARR[i]’ and ‘ARR[j]’ will be (j - i) * (min('ARR[i]', 'ARR[j]').
  4. This way, we will explore all possible containers and then take the maximum area out of all the containers.
  5. Return the maximum area of out of all possible containers.
Time Complexity

O(N ^ 2), where ‘N’ is the number of elements in the given array/list.

 

Since we are pivoting each element once and for each element we are traversing the given array/list till the end. So there will be a total of ‘N’ elements to pivot and then to traverse the given array/list will take O(N) time. Thus, the overall time complexity will be O(N^2).

Space Complexity

O(1)

 

Since we are not using any extra space and thus the space complexity will be O(1).

Code Solution
(100% EXP penalty)
Container with Maximum Water
Full screen
Console