Water Bottles II - Problem

You are given two integers numBottles and numExchange.

numBottles represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:

  • Drink any number of full water bottles turning them into empty bottles.
  • Exchange numExchange empty bottles with one full water bottle. Then, increase numExchange by one.

Note: You cannot exchange multiple batches of empty bottles for the same value of numExchange. For example, if numBottles == 3 and numExchange == 1, you cannot exchange 3 empty water bottles for 3 full bottles.

Return the maximum number of water bottles you can drink.

Input & Output

Example 1 — Basic Case
$ Input: numBottles = 13, numExchange = 6
Output: 15
💡 Note: Drink 13 bottles → 13 empties. Exchange 6 empties for 1 new bottle (cost becomes 7), leaving 7+1=8 empties. Drink 1 bottle → 8 empties. Exchange 7 empties for 1 new bottle (cost becomes 8), leaving 1+1=2 empties. Drink 1 bottle → 2 empties. Cannot exchange anymore (need 8, have 2). Total: 13+1+1=15
Example 2 — Higher Exchange Cost
$ Input: numBottles = 10, numExchange = 3
Output: 13
💡 Note: Drink 10 → 10 empties. Exchange 9 for 3 (cost=3), 1 empty left + 3 new = 4 empties total. Drink 3 → 7 empties. Exchange 4 for 1 (cost=4), 3 left + 1 new = 4 empties. Drink 1 → 5 empties. Exchange 5 for 1 (cost=5), 0 left + 1 new = 1 empty. Drink 1 → 1 empty. Total: 10+3+1+1=15
Example 3 — No Exchanges Possible
$ Input: numBottles = 2, numExchange = 4
Output: 2
💡 Note: Drink 2 bottles → 2 empties. Cannot exchange (need 4, have 2). Total: 2

Constraints

  • 1 ≤ numBottles ≤ 100
  • 1 ≤ numExchange ≤ 100

Visualization

Tap to expand
Water Bottles II: Exchange ProcessStart13 bottlesDrink All13 emptiesExchangeCost: 6→7→8Result15 totalRound 1: 13 empties ÷ 6 = 2 new bottles (1 empty remaining)Round 2: 3 empties ÷ 7 = 0 new bottles (3 empties remaining)Final: 13 (initial) + 2 (from exchanges) = 15 bottles drunkKey: Exchange cost increases after each round!
Understanding the Visualization
1
Input
numBottles=13 initial bottles, numExchange=6 exchange rate
2
Process
Drink bottles, exchange empties with increasing cost
3
Output
Total bottles drunk = 15
Key Takeaway
🎯 Key Insight: The exchange cost increases after each round, making fewer exchanges possible over time
Asked in
Google 15 Amazon 12
12.0K Views
Medium Frequency
~15 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen