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.Â
Examples:
Input: N = 9, E = 3
Output: 13
Explanation:
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
Output: 8
Explanation:
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:
C++
// C++ program for the// above approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the maximum// bottles that can be emptiedint maxBottles(int n, int e){Â Â Â Â int s = 0, b = 0;Â Â Â Â int a = n;Â
    // Iterate until a   // is non-zero    while (a != 0) {               // Add the number of       // bottles that are emptied        s = s + a;Â
        // Update a after        // exchanging empty bottles        a = (a + b) / e;Â
        // Stores the number of bottles        // left after the exchange        b = n - (a * e);        n = a + b;    }Â
    // Return the answer    return s;}Â
// Driver Codeint main(){Â Â Â Â int n = 9, e = 3;Â
    // Function call    int s = maxBottles(n, e);    cout << s << endl;} |
C
// C program for the above approach#include <stdio.h>Â
// Function to find the maximum// bottles that can be emptiedint maxBottles(int n, int e){Â Â Â Â int s = 0, b = 0;Â Â Â Â int a = n;Â
    // Iterate until a// is non-zero    while (a != 0) {             // Add the number of    // bottles that are emptied        s = s + a;Â
        // Update a after        // exchanging empty bottles        a = (a + b) / e;Â
        // Stores the number of bottles        // left after the exchange        b = n - (a * e);        n = a + b;    }Â
    // Return the answer    return s;}Â
// Driver Codeint main(){Â Â Â Â int n = 9, e = 3;Â
    // Function call    int s = maxBottles(n, e);    printf("%d",s);}Â
// This code is contributed by allwink45. |
Java
// Java program for the// above approachimport java.util.*;Â
class GFG{Â
// Function to find the maximum// bottles that can be emptiedstatic int maxBottles(int n, int e){Â Â Â Â int s = 0, b = 0;Â Â Â Â int a = n;Â
    // Iterate until a     // is non-zero    while (a != 0)     {             // Add the number of         // bottles that are emptied        s = s + a;Â
        // Update a after        // exchanging empty bottles        a = (a + b) / e;Â
        // Stores the number of bottles        // left after the exchange        b = n - (a * e);        n = a + b;    }Â
    // Return the answer    return s;}Â
// Driver Codepublic static void main(String[] args){Â Â Â Â int n = 9, e = 3;Â
    // Function call    int s = maxBottles(n, e);         System.out.print(s + "\n");}}Â
// This code is contributed by 29AjayKumar |
Python3
# Python3 program for the# above approachÂ
# Function to find the maximum# bottles that can be emptieddef maxBottles(n, e):Â Â Â Â Â Â Â Â Â s = 0Â Â Â Â b = 0Â Â Â Â a = nÂ
    # Iterate until a    # is non-zero    while (a != 0):Â
        # Add the number of        # bottles that are emptied        s = s + aÂ
        # Update a after        # exchanging empty bottles        a = (a + b) // eÂ
        # Stores the number of bottles        # left after the exchange        b = n - (a * e)        n = a + bÂ
    # Return the answer    return sÂ
# Driver Codeif __name__ == '__main__':Â Â Â Â Â Â Â Â Â n = 9Â Â Â Â e = 3Â
    # Function call    s = maxBottles(n, e)    print(s)Â
# This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System;Â
class GFG{Â
// Function to find the maximum// bottles that can be emptiedstatic int maxBottles(int n, int e){Â Â Â Â int s = 0, b = 0;Â Â Â Â int a = n;Â
    // Iterate until a     // is non-zero    while (a != 0)     {             // Add the number of         // bottles that are emptied        s = s + a;Â
        // Update a after        // exchanging empty bottles        a = (a + b) / e;Â
        // Stores the number of bottles        // left after the exchange        b = n - (a * e);        n = a + b;    }Â
    // Return the answer    return s;}Â
// Driver Codepublic static void Main(){Â Â Â Â int n = 9, e = 3;Â
    // Function call    int s = maxBottles(n, e);         Console.Write(s + "\n");}}Â
// This code is contributed by code_hunt |
Javascript
<script>Â
// javascript program for the// above approachÂ
// Function to find the maximum// bottles that can be emptiedfunction maxBottles(n, e){Â Â Â Â var s = 0, b = 0;Â Â Â Â var a = n;Â
    // Iterate until a     // is non-zero    while (a != 0)     {             // Add the number of         // bottles that are emptied        s = s + a;Â
        // Update a after        // exchanging empty bottles        a = (a + b) / e;Â
        // Stores the number of bottles        // left after the exchange        b = n - (a * e);        n = a + b;    }Â
    // Return the answer    return s;}Â
// Driver CodeÂ
var n = 9, e = 3;Â
// Function callvar s = maxBottles(n, e);Â
document.write(parseInt(s) + "\n");Â
// This code contributed by Princi Singh Â
</script> |
Output:
13
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



