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.
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)
Alternate Solution :
It can be solved mathematically with a simple formula:
Let’s Assume C>L. Let d be the amount of days after the Lth when the tank become empty.During that time, there will be (d-1)refills and d withdrawals.
Hence we need to solve this equation :
Sum of all withdrawals is a sum of arithmetic progression,therefore :
Discriminant = 1+8(C-L)>0,because C>L.
Skipping the negative root, we get the following formula:
Therefore, the final alwer is:
Thanks to Andrey Khayrutdinov for suggesting this solution.
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximize number of days for which P chocolates can be distributed consecutively to N people
- Search in an array of strings where non-empty strings are sorted
- Numbers that are bitwise AND of at least one non-empty sub-array
- Find the number of different numbers in the array after applying the given operation q times
- Find the minimum number of rectangles left after inserting one into another
- Largest number N which can be reduced to 0 in K steps
- Repeatedly search an element by doubling it after every successful search
- Maximize distance between any two consecutive 1's after flipping M 0's
- Minimum K such that sum of array elements after division by K does not exceed S
- Change in Median of given array after deleting given elements
- Count distinct elements after adding each element of First Array with Second Array
- Maximize median after doing K addition operation on the Array
- Find value after N operations to remove N characters of string S with given constraints
- Find the maximum element in an array which is first increasing and then decreasing
- Find the prime numbers which can written as sum of most consecutive primes
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Minimum in an array which is first decreasing then increasing
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Length of longest subarray in which elements greater than K are more than elements not greater than K
Improved By : jit_t