Situatie
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
Solutie
// 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; }

Leave A Comment?