Water Bottles - Problem

Imagine you're in a desert with a limited supply of water bottles, but there's a clever recycling program! You start with numBottles full water bottles. After drinking each bottle, it becomes empty, but here's the twist: you can exchange empty bottles for new full ones!

The exchange rate is simple: every numExchange empty bottles can be traded for 1 new full bottle. This means you can potentially drink more water than you initially had by being smart about the exchanges.

Goal: Find the maximum total number of water bottles you can drink.

Example: If you have 9 bottles and can exchange every 3 empty bottles for 1 full bottle, you can drink all 9 initially, then exchange the 9 empties for 3 new bottles, drink those 3, and exchange them for 1 more bottle. Total: 9 + 3 + 1 = 13 bottles!

Input & Output

example_1.py โ€” Basic Exchange
$ Input: numBottles = 9, numExchange = 3
โ€บ Output: 13
๐Ÿ’ก Note: You can drink 9 bottles initially. With 9 empty bottles, exchange for 3 new bottles (9รท3=3). Drink those 3, getting 3 empties, exchange for 1 more bottle (3รท3=1). Drink that 1 bottle. Total: 9 + 3 + 1 = 13.
example_2.py โ€” Higher Exchange Rate
$ Input: numBottles = 15, numExchange = 4
โ€บ Output: 19
๐Ÿ’ก Note: Drink 15 initially. Exchange 15 empties for 3 new bottles (15รท4=3, remainder 3). Drink 3, now have 3+3=6 empties. Exchange for 1 bottle (6รท4=1, remainder 2). Drink 1, have 2+1=3 empties (insufficient). Total: 15 + 3 + 1 = 19.
example_3.py โ€” Minimal Case
$ Input: numBottles = 5, numExchange = 5
โ€บ Output: 6
๐Ÿ’ก Note: Drink 5 bottles initially, get 5 empties. Exchange all 5 empties for exactly 1 new bottle (5รท5=1). Drink that 1 bottle, get 1 empty (insufficient for more exchanges). Total: 5 + 1 = 6.

Visualization

Tap to expand
Water Bottle Exchange ProcessInitial State9 Full BottlesDRINKAfter Drinking9 Empty BottlesEXCHANGEAfter Exchange3 New Full Bottles(9 รท 3 = 3)Mathematical InsightFormula: Total = numBottles + (numBottles-1)/(numExchange-1)Example: 9 + (9-1)/(3-1) = 9 + 8/2 = 9 + 4 = 13๐ŸŽฏ Final ResultTotal Bottles Drunk: 139 (initial) + 3 (first exchange) + 1 (final exchange)
Understanding the Visualization
1
Start with bottles
You begin with some full water bottles
2
Drink and collect empties
After drinking, you accumulate empty bottles
3
Exchange at recycling station
Trade empty bottles for new full ones
4
Repeat until insufficient empties
Continue the cycle until you can't exchange anymore
Key Takeaway
๐ŸŽฏ Key Insight: Instead of simulating each exchange, we can calculate that every (numExchange-1) empty bottles gives us 1 additional drink, leading to the O(1) mathematical solution.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Single calculation using arithmetic operations

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant amount of variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค numBottles โ‰ค 100
  • 2 โ‰ค numExchange โ‰ค 100
  • numExchange > 1 (must be at least 2 for exchanges to make sense)
Asked in
Amazon 35 Apple 28 Google 22 Microsoft 18
52.5K Views
Medium Frequency
~12 min Avg. Time
2.1K 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