Problem

There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.

Examples

Example 1:

Input: numBottles = 9, numExchange = 3
Output: 13
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 9 + 3 + 1 = 13.

Lets assume is filled bottle, and is empty bottle. Lets denote D for drink and X for exchange, then this is how it will work.

Initial   
                               
     =D=>       =X=>     =D=>     =X=>     =D=>  
                               

Example 2:

Input: numBottles = 15, numExchange = 4
Output: 19
Explanation: You can exchange 4 empty bottles to get 1 full water bottle. 
Number of water bottles you can drink: 15 + 3 + 1 = 19.

Solution

Method 2 - Iterative

As long as there are at least numExchange bottles remaining, we can drink them and exchange the empty bottles for full ones.

Code

Java
class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        int ans = numBottles;
        while (numBottles >= numExchange) {
            int remainder = numBottles % numExchange;
            numBottles /= numExchange;
            ans += numBottles;
            numBottles += remainder;
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(log n), where n is number of bottles
  • 🧺 Space complexity: O(1)

Method 2 - Geometric Progression

We know that in Geometric Progression, when the progression is converging, then the formula is: $$ S_{\infty} = \frac{a}{1-r} $$ For eg. when r = 0.5 = 1/2, this is GP is converging.

These are the terms of GP for eg. 1:

t0 = 9  
t1 = 3
t2 = 1

So, we can see that a = 9, r = 1/3,

S = a / (1 - r) = 9 / (1 - 1/3) = 9 / 2/3 = 9 * 3 / 2 = 27 / 2 = 13

Note, answer is not 13.5, but 13, as we want 1 complete bottle, not half empty bottles.

So, this example worked, but we need to make sure we do integer division. So, lets derive the formula. Let n be initial bottles given, and k be number of bottles we exchange to get 1 full bottle. First exchange:

$$ \left\lfloor \frac{n}{k} \right\rfloor $$

Remaining Empty Bottles:

$$ n - \left( k \cdot \left\lfloor \frac{n}{k} \right\rfloor \right) $$ Number of Full Bottles After Each Exchange: $$ \sum_{i=0}^{\infty} \left\lfloor \frac{n_i}{k} \right\rfloor $$

The total number of bottles you can drink is the sum of the initial bottles and the total number of full bottles obtained through exchanges. To simplify this calculation, we use the formula:

$$ \text{Total} = n + \left\lfloor \sum_{i=0}^{\infty} \left\lfloor \frac{n}{k} \right\rfloor \right\rfloor $$

To derive the total exchanges, realize that every additional bottle obtained through exchanges adds: $$ \frac{n-1}{k-1} $$

Simplified Geometric Series - Combining all, the total number of bottles you can drink simplifies to:

$$ \text{Total} = n + \frac{n-1}{k-1} $$

Code

Java
class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        return numBottles + (numBottles - 1) / (numExchange - 1);
    }
}

Complexity

  • ⏰ Time complexity: O(1)
  • 🧺 Space complexity: O(1)