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= 3Output:13Explanation:

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 = 5Output:8Explanation:

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 emptied ` `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 Code ` `int` `main() ` `{ ` ` ` `int` `n = 9, e = 3; ` ` ` ` ` `// Function call ` ` ` `int` `s = maxBottles(n, e); ` ` ` `cout << s << endl; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the ` `// above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// bottles that can be emptied ` `static` `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 Code ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the ` `# above approach ` ` ` `# Function to find the maximum ` `# bottles that can be emptied ` `def` `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 Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `9` ` ` `e ` `=` `3` ` ` ` ` `# Function call ` ` ` `s ` `=` `maxBottles(n, e) ` ` ` `print` `(s) ` ` ` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// bottles that can be emptied ` `static` `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 Code ` `public` `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 ` |

*chevron_right*

*filter_none*

**Output:**

13

**Time Complexity: **O(N)**Auxiliary Space:** O(1)

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.

## Recommended Posts:

- Program to check if water tank overflows when n solid balls are dipped in the water tank
- Minimum number of bottles required to fill K glasses
- Minimum number of Bottles visible when a bottle can be enclosed inside another Bottle
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Count of vessels completely filled after a given time
- Count 1s in binary matrix having remaining indices of its row and column filled with 0s
- Program to check if tank will overflow, underflow or filled in given time
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Number of containers that can be filled in the given time
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Find the element at the specified index of a Spirally Filled Matrix
- Maximize cost to empty given array by repetitively removing K array elements
- Program to find amount of water in a given glass
- Measure one litre using two vessels and infinite water supply
- Container with Most Water
- Speed of boat in still water from speed of stream and times taken
- Water Connection Problem
- Water drop problem
- Maximum litres of water that can be bought with N Rupees
- Water Game

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.