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 denoting whether the X ml of water has to be poured or drinked based on below conditions: 1. if T = 0, Pour X ml (millilitres) of water in the glass 2. if T = 1, Drink X ml of water from the glass
Examples:
Input: N = 4,
0 1
0 1
0 1
1 3
Output: 3
Explanation:
The glass initially has 0 ml of water.
The maximum value is obtained
after the first 3 operations.
Input: N = 2
1 15
1 24
Output: 39
Approach THe approach can be simply put as adding up the volumes whenever T is 0 and finding the maximum of this volume. Implementation:
C++
#include<iostream>
using namespace std;
int largest_volume( int n, int *t, int *x)
{
int minValue = 100000000;
int maxValue = 0;
int c = 0;
for ( int i = 0; i < n; i++)
{
if (t[i] == 0)
{
c += x[i];
maxValue = max(maxValue, c);
}
else
{
c -= x[i];
minValue = min(minValue, c);
}
}
return maxValue - minValue;
}
int main()
{
int n = 4;
int t[4] = {0};
int x[4] = {0};
t[0] = 0;
x[0] = 1;
t[1] = 0;
x[1] = 1;
t[2] = 0;
x[2] = 1;
t[3] = 1;
x[3] = 3;
int ans = largest_volume(n, t, x);
cout<< ans;
return 0;
}
|
Java
class GFG {
static int largest_volume( int n, int [] t, int [] x)
{
int min = 100000000 ;
int max = 0 ;
int c = 0 ;
for ( int i = 0 ; i < n; i++) {
if (t[i] == 0 ) {
c += x[i];
max = Math.max(max, c);
}
else {
c -= x[i];
min = Math.min(min, c);
}
}
return max - min;
}
public static void main(String[] args)
{
int n = 4 ;
int [] t = new int [ 4 ];
int [] x = new int [ 4 ];
t[ 0 ] = 0 ;
x[ 0 ] = 1 ;
t[ 1 ] = 0 ;
x[ 1 ] = 1 ;
t[ 2 ] = 0 ;
x[ 2 ] = 1 ;
t[ 3 ] = 1 ;
x[ 3 ] = 3 ;
int ans = largest_volume(n, t, x);
System.out.println(ans);
}
}
|
Python3
def largest_volume(n, t, x):
Min = 100000000
Max = 0
c = 0
for i in range ( 0 , n):
if t[i] = = 0 :
c + = x[i]
Max = max ( Max , c)
else :
c - = x[i]
Min = min ( Min , c)
return Max - Min
if __name__ = = "__main__" :
n = 4
t = [ 0 , 0 , 0 , 1 ]
x = [ 1 , 1 , 1 , 3 ]
ans = largest_volume(n, t, x)
print (ans)
|
C#
using System;
class GFG
{
static int largest_volume( int n, int [] t, int [] x)
{
int min = 100000000;
int max = 0;
int c = 0;
for ( int i = 0; i < n; i++)
{
if (t[i] == 0)
{
c += x[i];
max = Math.Max(max, c);
}
else
{
c -= x[i];
min = Math.Min(min, c);
}
}
return max - min;
}
public static void Main()
{
int n = 4;
int [] t = new int [4];
int [] x = new int [4];
t[0] = 0;
x[0] = 1;
t[1] = 0;
x[1] = 1;
t[2] = 0;
x[2] = 1;
t[3] = 1;
x[3] = 3;
int ans = largest_volume(n, t, x);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function largest_volume(n, t, x)
{
let minValue = 100000000;
let maxValue = 0;
let c = 0;
for (let i = 0; i < n; i++)
{
if (t[i] == 0)
{
c += x[i];
maxValue = Math.max(maxValue, c);
}
else
{
c -= x[i];
minValue = Math.min(minValue, c);
}
}
return maxValue - minValue;
}
const n = 4;
const t = Array(n).fill(0);
const x = Array(n).fill(0);
t[0] = 0;
x[0] = 1;
t[1] = 0;
x[1] = 1;
t[2] = 0;
x[2] = 1;
t[3] = 1;
x[3] = 3;
let ans = largest_volume(n, t, x);
document.write(ans);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...