Water Bottles 2 — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Water Bottles 2
Detailed explanation and reference materials
Problem Overview

Water Bottles II

Medium


Hint

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:
  1. Drink any number of full water bottles turning them into empty bottles.
  2. 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.


Example 1

Input:

numBottles = 13, numExchange = 6

Output:

15

Step-by-step table (Full / Empty / numExchange / cumulative drunk):

StepActionFull (after)Empty (after)numExchange (used → new / or current)Drunk (cumulative)
0Start13060
1Drink 13013613
2Exchange (use 6)176 → 713
3Exchange (use 7)207 → 813
4Drink 202815

Final answer: 15


Example 2

Input:

numBottles = 10, numExchange = 3

Output:

13

Step-by-step table (Full / Empty / numExchange / cumulative drunk):

StepActionFull (after)Empty (after)numExchange (used → new / or current)Drunk (cumulative)
0Start10030
1Drink 10010310
2Exchange (use 3)173 → 410
3Exchange (use 4)234 → 510
4Drink 205512
5Exchange (use 5)105 → 612
6Drink 101613

Final answer: 13


Constraints

  • 1 <= numBottles <= 100
  • 1 <= numExchange <= 100
— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
maths
simulation
leetcode-problem-of-the-day
java
Reference Link
https://leetcode.com/problems/water-bottles-ii/description/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.