Maximize count of empty water bottles from N filled bottles
Given two integers N and E, where N represents the number of full water bottles and E represents the number of empty bottles that can be exchanged for a full water bottle. The task is to find the maximum number of water bottles that can be emptied.
Input: N = 9, E = 3
Initially, there are 9 fully filled water bottles.
All of them are emptied to obtain 9 empty bottles. Therefore, count = 9
Then exchange 3 bottles at a time for 1 fully filled bottle.
Therefore, 3 fully filled bottles are obtained.
Then those 3 bottles are emptied to get 3 empty bottles. Therefore, count = 9 + 3 = 12
Then those 3 bottles are exchanged for 1 full bottle which is emptied.
Therefore, count = 9 + 3 + 1 = 13.
Input: N = 7, E = 5
Empty the 7 fully filled water bottles. Therefore, count = 7
Then exchange 5 bottles to obtain 1 fully filled bottle. Then empty that bottle.
Therefore, count = 7 + 1 = 8.
Approach: The following steps have to be followed to solve the problem:
- Store the value of N in a temporary variable, say a.
- Initialize a variable, say s, to store the count of empty bottles and another variable, say b, to store the count of bottles remaining after exchange.
- Now, iterate until a is not equal to 0 and increment s by a, as it will be the minimum number of bottles that have been emptied.
- Update the following values:
- a to (a + b) / e
- b to N – (a * e)
- N to (a+b)
- Return s as the required answer.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)