Container with Most Water
Last Updated :
16 Jan, 2025
Given an array arr[] of non-negative integers, where each element arr[i] represents the height of the vertical lines, find the maximum amount of water that can be contained between any two lines, together with the x-axis.
Examples :
Input: arr[] = [1, 5, 4, 3]
Output: 6
Explanation: 5 and 3 are 2 distance apart. So the size of the base = 2. Height of container = min(5, 3) = 3. So total area = 3 * 2 = 6.
Input: arr[] = [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.
Input: arr[] = [2, 1, 8, 6, 4, 6, 5, 5]
Output: 25
Explanation: 8 and 5 are 5 distance apart. So the size of the base = 5. Height of container = min(8, 5) = 5. So, total area = 5 * 5 = 25.
[Naive Approach] Finding all possible boundaries – O(n^2) Time and O(1) Space
The idea is to iterate over all pair of lines in the array. For each line arr[i], consider it as the left boundary and then iterate over all subsequent lines arr[j] (j > i) to consider them as them as the right boundary. For each pair of boundaries, calculate the water that can be contained between them by the formula min(arr[i], arr[j]) * (j – i). The result will be the maximum amount of water found across all pairs.
C++
// C++ Program to find the maximum amount of water
// by iterating over all possible boundaries
#include <vector>
#include <iostream>
using namespace std;
int maxWater(vector<int> &arr) {
int n = arr.size();
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Calculate the amount of water
int amount = min(arr[i], arr[j]) * (j - i);
// Keep track of maximum amount of water
res = max(amount, res);
}
}
return res;
}
int main() {
vector<int> arr = {2, 1, 8, 6, 4, 6, 5, 5};
cout << maxWater(arr);
}
C
// C Program to find the maximum amount of water
// by iterating over all possible boundaries
#include <stdio.h>
int maxWater(int arr[], int n) {
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Calculate the amount of water
int amount = (arr[i] < arr[j] ? arr[i] : arr[j]) * (j - i);
// Keep track of maximum amount of water
res = (amount > res ? amount : res);
}
}
return res;
}
int main() {
int arr[] = {2, 1, 8, 6, 4, 6, 5, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", maxWater(arr, n));
return 0;
}
Java
// Java Program to find the maximum amount of water
// by iterating over all possible boundaries
import java.util.*;
class GfG {
static int maxWater(int[] arr) {
int n = arr.length;
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Calculate the amount of water
int amount = Math.min(arr[i], arr[j]) * (j - i);
// Keep track of maximum amount of water
res = Math.max(amount, res);
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {2, 1, 8, 6, 4, 6, 5, 5};
System.out.println(maxWater(arr));
}
}
Python
# Python Program to find the maximum amount of water
# by iterating over all possible boundaries
def maxWater(arr):
n = len(arr)
res = 0
for i in range(n):
for j in range(i + 1, n):
# Calculate the amount of water
amount = min(arr[i], arr[j]) * (j - i)
# Keep track of maximum amount of water
res = max(amount, res)
return res
if __name__ == "__main__":
arr = [2, 1, 8, 6, 4, 6, 5, 5]
print(maxWater(arr))
C#
// C# Program to find the maximum amount of water
// by iterating over all possible boundaries
using System;
class GfG {
static int maxWater(int[] arr) {
int n = arr.Length;
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Calculate the amount of water
int amount = Math.Min(arr[i], arr[j]) * (j - i);
// Keep track of maximum amount of water
res = Math.Max(amount, res);
}
}
return res;
}
static void Main() {
int[] arr = { 2, 1, 8, 6, 4, 6, 5, 5 };
Console.WriteLine(maxWater(arr));
}
}
JavaScript
// JavaScript Program to find the maximum amount of water
// by iterating over all possible boundaries
function maxWater(arr) {
let n = arr.length;
let res = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
// Calculate the amount of water
let amount = Math.min(arr[i], arr[j]) * (j - i);
// Keep track of maximum amount of water
res = Math.max(amount, res);
}
}
return res;
}
// Driver Code
let arr = [2, 1, 8, 6, 4, 6, 5, 5];
console.log(maxWater(arr));
Time Complexity: O(n^2)
Auxiliary Space: O(1)
[Expected Approach] Using Two Pointers – O(n) Time and O(1) Space
The idea is to maintain two pointers: left pointer at the beginning of the array and right pointer at the end of the array. These pointers act as the container’s sides so we can calculate the amount of water stored between them using the formula: min(arr[left], arr[right]) * (right – left).
After calculating the amount of water for the given sides, we can have three cases:
- arr[left] < arr[right]: This means that we have calculated the water stored for the container of height arr[left], so increment left by 1.
- arr[left] >= arr[right]: This means that we have calculated the water stored for the container of height arr[right], so decrement right by 1.
Repeat the above process till left is less than right keeping track of the maximum water stored as the result.
Why are we moving the pointer which is pointing to the shorter line?
We are moving the pointer pointing to the shorter line to potentially find a taller line and increase the container’s height. Moving the pointer to the taller line would only reduce the width, but the height cannot increase because of the shorter line, thus decreasing the amount of water.
C++
// C++ Program to find the maximum amount of water in the
// container using Two Pointer Technique
#include <vector>
#include <iostream>
using namespace std;
int maxWater(vector<int> &arr) {
int left = 0, right = arr.size() - 1;
int res = 0;
while(left < right) {
// Find the water stored in the container between
// arr[left] and arr[right]
int water = min(arr[left], arr[right]) * (right - left);
res = max(res, water);
if(arr[left] < arr[right])
left += 1;
else
right -= 1;
}
return res;
}
int main() {
vector<int> arr = {2, 1, 8, 6, 4, 6, 5, 5};
cout << maxWater(arr);
}
Java
// Java Program to find the maximum amount of water in the
// container using Two Pointer Technique
import java.util.*;
class GfG {
static int maxWater(int[] arr) {
int left = 0, right = arr.length - 1;
int res = 0;
while (left < right) {
// Find the water stored in the container between
// arr[left] and arr[right]
int water = Math.min(arr[left], arr[right]) * (right - left);
res = Math.max(res, water);
if (arr[left] < arr[right])
left += 1;
else
right -= 1;
}
return res;
}
public static void main(String[] args) {
int[] arr = {2, 1, 8, 6, 4, 6, 5, 5};
System.out.println(maxWater(arr));
}
}
Python
# Python Program to find the maximum amount of water in the
# container using Two Pointer Technique
def maxWater(arr):
left = 0
right = len(arr) - 1
res = 0
while left < right:
# Find the water stored in the container between
# arr[left] and arr[right]
water = min(arr[left], arr[right]) * (right - left)
res = max(res, water)
if arr[left] < arr[right]:
left += 1
else:
right -= 1
return res
if __name__ == "__main__":
arr = [2, 1, 8, 6, 4, 6, 5, 5]
print(maxWater(arr))
C#
// C# Program to find the maximum amount of water in the
// container using Two Pointer Technique
using System;
using System.Collections.Generic;
class GfG {
static int maxWater(int[] arr) {
int left = 0, right = arr.Length - 1;
int res = 0;
while (left < right) {
// Find the water stored in the container between
// arr[left] and arr[right]
int water = Math.Min(arr[left], arr[right]) * (right - left);
res = Math.Max(res, water);
if (arr[left] < arr[right])
left += 1;
else
right -= 1;
}
return res;
}
static void Main() {
int[] arr = { 2, 1, 8, 6, 4, 6, 5, 5 };
Console.WriteLine(maxWater(arr));
}
}
JavaScript
// JavaScript Program to find the maximum amount of water in the
// container using Two Pointer Technique
function maxWater(arr) {
let left = 0, right = arr.length - 1;
let res = 0;
while (left < right) {
// Find the water stored in the container between
// arr[left] and arr[right]
let water = Math.min(arr[left], arr[right]) * (right - left);
res = Math.max(res, water);
if (arr[left] < arr[right])
left += 1;
else
right -= 1;
}
return res;
}
// Driver Code
let arr = [2, 1, 8, 6, 4, 6, 5, 5];
console.log(maxWater(arr));
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Container with Most Water
Given an array arr[] of non-negative integers, where each element arr[i] represents the height of the vertical lines, find the maximum amount of water that can be contained between any two lines, together with the x-axis. Examples : Input: arr[] = [1, 5, 4, 3]Output: 6Explanation: 5 and 3 are 2 dist
9 min read
Puzzle 8 | (Find the Jar with contaminated pills)
Puzzle: You have 5 jars of pills. Each pill weighs 10 grams, except for contaminated pills contained in one jar, where each pill weighs 9 grams. Given a scale, how could you tell which jar had the contaminated pills in just one measurement? Solution: To find the contaminated Jar, follow this step-wi
1 min read
The Water Jug Problem - Count Min Steps
You are given a m liter jug and a n liter jug where 0 < m < n. Both the jugs are initially empty. The jugs donât have markings to allow measuring smaller quantities. You have to use the jugs to measure d liters of water where d < n. Determine the minimum no of operations to be performed to
14 min read
Minimum cost to provide water
Given an integer N, where N denotes the number of villages numbered 1 to N, an array wells[] where wells[i] denotes the cost to build a water well in the i'th city, a 2D array pipes in form of [X Y C] which denotes that the cost to connect village X and Y with water pipes is C. Your task is to provi
11 min read
Maximizing Smallest Flower Height in Garden with Watering Constraint
You have a garden with n flowers lined up in a row. The height of ith flower is ai units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the he
11 min read
Trapping Rain Water Problem - Tutorial with Illustrations
Trapping Rainwater Problem states that given an array of n non-negative integers arr[] representing an elevation map where the width of each bar is 1, compute how much water it can trap after rain. Examples: Input: arr[] = [3, 0, 1, 0, 4, 0, 2]Output: 10Explanation: The expected rainwater to be trap
15+ min read
Water Game
Given an empty glass, this glass has to be filled with water and the task is to find the maximum amount of water that the glass has held at any moment. Given conditions: The program takes an input N denoting the number of steps. Each step consists of two inputs T and X where T is the flag condition
6 min read
Trapping Rain Water in a Matrix
Given a matrix arr[][] of dimension M*N consisting of positive integers, where arr[i][j] represents the height of each unit cell, the task is to find the total volume of water trapped in the matrix after rain. Examples: Input: arr[][] = {{4, 2, 7}, {2, 1, 10}, {5, 10, 2}} Output: 1Explanation:The ra
14 min read
K'th largest element in a stream
Given an infinite stream of integers, find the Kth largest element at any point of time. Note: Here we have a stream instead of a whole array and we are allowed to store only K elements. Examples: Input: stream[] = {10, 20, 11, 70, 50, 40, 100, 5, . . .}, K = 3Output: {_, _, 10, 11, 20, 40, 50, 50,
14 min read
Select a random number from stream, with O(1) space
Given a stream of numbers, generate a random number from the stream. You are allowed to use only O(1) space and the input is in the form of a stream, so can't store the previously seen numbers. So how do we generate a random number from the whole stream such that the probability of picking any numbe
11 min read