Number of days after which tank will become empty

Given a tank with capacity C liters which is completely filled in starting. Everyday tank is filled with l liters of water and in the case of overflow extra water is thrown out. Now on i-th day i liters of water is taken out for drinking. We need to find out the day at which tank will become empty the first time.

Examples:

Input : Capacity = 5        
        l = 2
Output : 4
At the start of 1st day, water in tank = 5    
and at the end of the 1st day = (5 - 1) = 4
At the start of 2nd day, water in tank = 4 + 2 = 6
but tank capacity is 5 so water = 5
and at the end of the 2nd day = (5 - 2) = 3
At the start of 3rd day, water in tank = 3 + 2 = 5
and at the end of the 3rd day = (5 - 3) = 2
At the start of 4th day, water in tank = 2 + 2 = 4
and at the end of the 4th day = (4 - 4) = 0
    So final answer will be 4

We can see that tank will be full for starting (l + 1) days because water taken out is less than water being filled. After that, each day water in the tank will be decreased by 1 more liter and on (l + 1 + i)th day (C – (i)(i + 1) / 2) liter water will remain before taking drinking water.
Now we need to find a minimal day (l + 1 + K), in which even after filling the tank by l liters we have water less than l in tank i.e. on (l + 1 + K – 1)th day tank becomes empty so our goal is to find minimum K such that,
C – K(K + 1) / 2 <= l

We can solve above equation using binary search and then (l + K) will be our answer. Total time complexity of solution will be O(log C)

C++

// C/C++ code to find number of days after which
// tank will become empty
#include <bits/stdc++.h>
using namespace std;

// Utility method to get sum of first n numbers
int getCumulateSum(int n)
{
    return (n * (n + 1)) / 2;
}

// Method returns minimum number of days after 
// which tank will become empty
int minDaysToEmpty(int C, int l)
{
    // if water filling is more than capacity then
    // after C days only tank will become empty
    if (C <= l) 
        return C;    

    // initialize binary search variable
    int lo = 0;
    int hi = 1e4;
    int mid;

    // loop until low is less than high
    while (lo < hi) {
        mid = (lo + hi) / 2;

        // if cumulate sum is greater than (C - l) 
        // then search on left side
        if (getCumulateSum(mid) >= (C - l)) 
            hi = mid;
        
        // if (C - l) is more then search on
        // right side
        else 
            lo = mid + 1;        
    }

    // final answer will be obtained by adding
    // l to binary search result
    return (l + lo);
}

// Driver code to test above methods
int main()
{
    int C = 5;
    int l = 2;

    cout << minDaysToEmpty(C, l) << endl;
    return 0;
}

Java

// Java code to find number of days after which
// tank will become empty
public class Tank_Empty {
    
    // Utility method to get sum of first n numbers
    static int getCumulateSum(int n)
    {
        return (n * (n + 1)) / 2;
    }
     
    // Method returns minimum number of days after 
    // which tank will become empty
    static int minDaysToEmpty(int C, int l)
    {
        // if water filling is more than capacity then
        // after C days only tank will become empty
        if (C <= l) 
            return C;    
     
        // initialize binary search variable
        int lo = 0;
        int hi = (int)1e4;
        int mid;
     
        // loop until low is less than high
        while (lo < hi) {
            
            mid = (lo + hi) / 2;
     
            // if cumulate sum is greater than (C - l) 
            // then search on left side
            if (getCumulateSum(mid) >= (C - l)) 
                hi = mid;
             
            // if (C - l) is more then search on
            // right side
            else
                lo = mid + 1;        
        }
     
        // final answer will be obtained by adding
        // l to binary search result
        return (l + lo);
    }
     
    // Driver code to test above methods
    public static void main(String args[])
    {
        int C = 5;
        int l = 2;
     
        System.out.println(minDaysToEmpty(C, l));
    }
}
// This code is contributed by Sumit Ghosh


Output:

4

This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.