Water Bottles - 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.

Input & Output

Example 1 — Basic Case
$ Input: numBottles = 9, numExchange = 3
Output: 13
💡 Note: Drink 9 bottles → 9 empties. Exchange 9÷3=3 new bottles → drink them → 3 empties. Exchange 3÷3=1 new bottle → drink it → 1 empty. Can't exchange 1 empty (need 3). Total: 9+3+1=13.
Example 2 — Different Exchange Rate
$ Input: numBottles = 15, numExchange = 4
Output: 19
💡 Note: Drink 15 → 15 empties. Exchange 15÷4=3 new, 3 empties left → total 6 empties. Exchange 6÷4=1 new, 2 empties left → total 3 empties. Can't exchange (need 4). Total: 15+3+1=19.
Example 3 — Minimum Case
$ Input: numBottles = 5, numExchange = 5
Output: 6
💡 Note: Drink 5 bottles → 5 empties. Exchange 5÷5=1 new bottle → drink it → 1 empty. Can't exchange 1 empty (need 5). Total: 5+1=6.

Constraints

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

Visualization

Tap to expand
Water Bottles Problem INPUT Initial Full Bottles: 9 Exchange Rate: = numBottles=9 numExchange=3 ALGORITHM STEPS 1 Drink all bottles drunk=9, empty=9 2 Exchange 9/3 = 3 drunk=12, empty=3 3 Exchange 3/3 = 1 drunk=13, empty=1 4 Stop: empty < 3 Cannot exchange more Iteration Table Round Drink Empty Total 0 9 9 9 1 3 3 12 2 1 1 13 FINAL RESULT Total Bottles Consumed Output 13 Breakdown: 9 initial + 3 first exchange + 1 second = 13 OK Key Insight: Simulate the process: drink all full bottles, then repeatedly exchange empty bottles for full ones until you have fewer empties than the exchange rate. Track total drunk across all iterations. TutorialsPoint - Water Bottles | Optimal Solution
Asked in
Amazon 15 Microsoft 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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