Open In App

Water Game

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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++




// C++ implementation of the approach
#include<iostream> 
using namespace std;
  
// returns the largest volume
int largest_volume(int n, int *t, int *x)
{
  
    // arbitrarily large value
    int minValue = 100000000;
  
    // stores the maximum
    int maxValue = 0;
  
    // Current Volume of the glass
    int c = 0;
  
    for (int i = 0; i < n; i++) 
    {
  
        // if 1st operation is performed
        if (t[i] == 0) 
        {
  
            // increment with current x
            c += x[i];
  
            // take current max
            maxValue = max(maxValue, c);
        }
  
        // if 2nd operation is performed
        else 
        {
  
            // decrement with current x
            c -= x[i];
  
            // take current min
            minValue = min(minValue, c);
        }
    }
  
    // returns the largest difference
    return maxValue - minValue;
}
  
// Driver code
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;
  
    // Find the largest volume
    int ans = largest_volume(n, t, x);
  
    // Print the largest volume
    cout<< ans;
    return 0;
}
  
// This code is contributed by PrinciRaj1992 


Java




// Java implementation of the approach
  
class GFG {
  
    // returns the largest volume
    static int largest_volume(int n, int[] t, int[] x)
    {
  
        // arbitrarily large value
        int min = 100000000;
  
        // stores the maximum
        int max = 0;
  
        // Current Volume of the glass
        int c = 0;
  
        for (int i = 0; i < n; i++) {
  
            // if 1st operation is performed
            if (t[i] == 0) {
  
                // increment with current x
                c += x[i];
  
                // take current max
                max = Math.max(max, c);
            }
  
            // if 2nd operation is performed
            else {
  
                // decrement with current x
                c -= x[i];
  
                // take current min
                min = Math.min(min, c);
            }
        }
  
        // returns the largest difference
        return max - min;
    }
  
    // Driver code
    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;
  
        // Find the largest volume
        int ans = largest_volume(n, t, x);
  
        // Print the largest volume
        System.out.println(ans);
    }
}


Python3




# Python3 implementation of the approach 
  
# Returns the largest volume 
def largest_volume(n, t, x): 
  
    # Arbitrarily large value 
    Min = 100000000
  
    # Stores the maximum 
    Max = 0
  
    # Current Volume of the glass 
    c = 0
  
    for i in range(0, n): 
  
        # If 1st operation is performed 
        if t[i] == 0
  
            # Increment with current x 
            c += x[i] 
  
            # Take current max 
            Max = max(Max, c) 
  
        # If 2nd operation is performed 
        else
              
            # Decrement with current x 
            c -= x[i] 
  
            # Take current min 
            Min = min(Min, c) 
  
    # Returns the largest difference 
    return Max - Min
  
# Driver code 
if __name__ == "__main__":
      
    n = 4
  
    t = [0, 0, 0, 1]
    x = [1, 1, 1, 3
  
    # Find the largest volume 
    ans = largest_volume(n, t, x) 
  
    # Print the largest volume 
    print(ans) 
      
# This code is contributed by Rituraj Jain


C#




// C# implementation of the approach
using System; 
  
class GFG 
  
    // returns the largest volume 
    static int largest_volume(int n, int[] t, int[] x) 
    
  
        // arbitrarily large value 
        int min = 100000000; 
  
        // stores the maximum 
        int max = 0; 
  
        // Current Volume of the glass 
        int c = 0; 
  
        for (int i = 0; i < n; i++)
        
  
            // if 1st operation is performed 
            if (t[i] == 0)
            
  
                // increment with current x 
                c += x[i]; 
  
                // take current max 
                max = Math.Max(max, c); 
            
  
            // if 2nd operation is performed 
            else 
            
  
                // decrement with current x 
                c -= x[i]; 
  
                // take current min 
                min = Math.Min(min, c); 
            
        
  
        // returns the largest difference 
        return max - min; 
    
  
    // Driver code 
    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; 
  
        // Find the largest volume 
        int ans = largest_volume(n, t, x); 
  
        // Print the largest volume 
        Console.WriteLine(ans); 
    
  
// This code is contributed by 
// tufan_gupta2000


Javascript




<script>
    // Javascript implementation of the approach
      
    // returns the largest volume
    function largest_volume(n, t, x)
    {
      
        // arbitrarily large value
        let minValue = 100000000;
      
        // stores the maximum
        let maxValue = 0;
      
        // Current Volume of the glass
        let c = 0;
      
        for (let i = 0; i < n; i++)
        {
      
            // if 1st operation is performed
            if (t[i] == 0)
            {
      
                // increment with current x
                c += x[i];
      
                // take current max
                maxValue = Math.max(maxValue, c);
            }
      
            // if 2nd operation is performed
            else
            {
      
                // decrement with current x
                c -= x[i];
      
                // take current min
                minValue = Math.min(minValue, c);
            }
        }
      
        // returns the largest difference
        return maxValue - minValue;
    }
      
    // Driver code
        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;
      
        // Find the largest volume
        let ans = largest_volume(n, t, x);
      
        // Print the largest volume
        document.write(ans);
          
        // This code is contributed by Pushpesh Raj.
</script>


Output:

3

Time Complexity: O(n) 
Auxiliary Space: O(1)



Last Updated : 13 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads