Minimum Number of Operations to Move All Balls to Each Box — 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 Minimum Number of Operations to Move All Balls to Each Box
Detailed explanation and reference materials
Problem Overview

Problem: Minimum Number of Operations to Move All Balls to Each Box

You have n boxes represented as a binary string boxes, where:

  • '0' means the box is empty.
  • '1' means the box contains one ball.

You can move a ball from one box to an adjacent box in one operation. A box is adjacent if the difference in their indices is 1.

You need to calculate an array answer of size n where:

  • answer[i] is the minimum number of operations required to move all balls to the ith box, considering the initial positions of the balls.

Examples

Example 1:

Input:
boxes = "110"
Output:
[1, 1, 3]

Boxes

The number of operations required to move all balls to the 0th index box from the initial state = 1.

Boxes

The number of operations required to move all balls to the 1st index box from the initial state = 1.

Boxes

The number of operations required to move all balls to the 3rd index box from the initial state = 2.

Boxes

The total number of operations required to move all balls to each index box from the initial state is given by the array:

ans = [1, 1, 3]

Explanation:

  1. For the first box: Move one ball from the second box → 1 operation.
  2. For the second box: Move one ball from the first box → 1 operation.
  3. For the third box:
    • Move one ball from the first box → 2 operations.
    • Move one ball from the second box → 1 operation.
      Total = 3 operations.

Example 2:

Input:
boxes = "001011"
Output:
[11, 8, 5, 4, 3, 4]


Constraints

n=boxes.lengthn = \text{boxes.length} 1n20001 \leq n \leq 2000 boxes[i]{0,1}\text{boxes}[i] \in \{ 0 , 1 \} — Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
leetcode-problem-of-the-day
arrays
strings
java
Reference Link
https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/description/?envType=daily-question&envId=2025-01-06

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.