Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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:
numExchange empty bottles with one full water bottle. Then, increase numExchange by one.Note that 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.
Example 1:
Input: numBottles = 13, numExchange = 6 Output: 15 Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
Example 2:
Input: numBottles = 10, numExchange = 3 Output: 13 Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
Constraints:
1 <= numBottles <= 100 1 <= numExchange <= 100Problem summary: 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 that 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
13 6
10 3
water-bottles)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3100: Water Bottles II
class Solution {
public int maxBottlesDrunk(int numBottles, int numExchange) {
int ans = numBottles;
while (numBottles >= numExchange) {
numBottles -= numExchange;
++numExchange;
++ans;
++numBottles;
}
return ans;
}
}
// Accepted solution for LeetCode #3100: Water Bottles II
func maxBottlesDrunk(numBottles int, numExchange int) int {
ans := numBottles
for numBottles >= numExchange {
numBottles -= numExchange
numExchange++
ans++
numBottles++
}
return ans
}
# Accepted solution for LeetCode #3100: Water Bottles II
class Solution:
def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int:
ans = numBottles
while numBottles >= numExchange:
numBottles -= numExchange
numExchange += 1
ans += 1
numBottles += 1
return ans
// Accepted solution for LeetCode #3100: Water Bottles II
impl Solution {
pub fn max_bottles_drunk(mut num_bottles: i32, mut num_exchange: i32) -> i32 {
let mut ans = num_bottles;
while num_bottles >= num_exchange {
num_bottles -= num_exchange;
num_exchange += 1;
ans += 1;
num_bottles += 1;
}
ans
}
}
// Accepted solution for LeetCode #3100: Water Bottles II
function maxBottlesDrunk(numBottles: number, numExchange: number): number {
let ans = numBottles;
while (numBottles >= numExchange) {
numBottles -= numExchange;
++numExchange;
++ans;
++numBottles;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.