🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏
Problem: Minimum Operations to Move All Balls to Each BoxIntuitionTo calculate the minimum operations needed for all balls in a box to be moved to any other box
![image.png](https://assets.leetcode.com/users/images/6ef096a6-28c4-4d37-8f30-e53641242689_1736180221.6807263.png)



---

![1000056423.png](https://assets.leetcode.com/users/images/5f261bd8-846e-4efc-8dd9-f61263d39e77_1736181061.8888612.png)


---

### Problem: Minimum Operations to Move All Balls to Each Box  

### **Intuition**
To calculate the minimum operations needed for all balls in a box to be moved to any other box, we can use a **prefix sum-like approach**. Instead of recalculating operations for each box from scratch, we leverage results from previous calculations.  

The left side of a box keeps track of how many balls (ones) are moved to the left, while the right side tracks movements to the right. By adjusting these counts iteratively, we reduce redundant calculations.  

---

### **Approach**
1. Traverse the string `boxes` to calculate:
   - Initial `rightCount`: the number of '1's to the right of the first box.
   - Initial `totalOperations`: sum of all operations needed to move these balls to the first box.
2. Compute the operations for the first box and initialize left and right counts.
3. For each subsequent box:
   - Update operations for left and right balls iteratively using the results of the previous box.
   - Adjust left and right counts based on whether the current box contains a ball (`'1'`).
4. Repeat until all operations for each box are computed.

---

### **Complexity**
- **Time Complexity**:  
  $$O(n)$$ because the solution involves a single traversal of the string `boxes`.
- **Space Complexity**:  
  $$O(n)$$ to store the `operations` array.

---

### **Code Implementations**


```dart []
class Solution {
  List<int> minOperations(String boxes) {
    int n = boxes.length;
    List<int> operations = [];
    int leftCount = boxes[0] == "1" ? 1 : 0;
    int rightCount = 0;
    int totalOperations = 0;

    for (int i = 1; i < n; i++) {
      if (boxes[i] == "1") {
        rightCount++;
        totalOperations += i;
      }
    }

    operations.add(totalOperations);

    int leftOperations = 0;
    for (int i = 1; i < n; i++) {
      totalOperations -= rightCount;
      leftOperations += leftCount;
      operations.add(totalOperations + leftOperations);
      if (boxes[i] == "1") {
        leftCount++;
        rightCount--;
      }
    }

    return operations;
  }
}
```




```python []
class Solution:
    def minOperations(self, boxes: str) -> List[int]:
        n = len(boxes)
        operations = [0] * n
        left_count = 1 if boxes[0] == '1' else 0
        right_count = 0
        total_operations = 0

        for i in range(1, n):
            if boxes[i] == '1':
                right_count += 1
                total_operations += i

        operations[0] = total_operations
        left_operations = 0

        for i in range(1, n):
            total_operations -= right_count
            left_operations += left_count
            operations[i] = total_operations + left_operations

            if boxes[i] == '1':
                left_count += 1
                right_count -= 1

        return operations
```




```java []
class Solution {
    public int[] minOperations(String boxes) {
        int n = boxes.length();
        int[] operations = new int[n];
        int leftCount = boxes.charAt(0) == '1' ? 1 : 0;
        int rightCount = 0;
        int totalOperations = 0;

        for (int i = 1; i < n; i++) {
            if (boxes.charAt(i) == '1') {
                rightCount++;
                totalOperations += i;
            }
        }

        operations[0] = totalOperations;
        int leftOperations = 0;

        for (int i = 1; i < n; i++) {
            totalOperations -= rightCount;
            leftOperations += leftCount;
            operations[i] = totalOperations + leftOperations;

            if (boxes.charAt(i) == '1') {
                leftCount++;
                rightCount--;
            }
        }

        return operations;
    }
}
```




```cpp []
class Solution {
public:
    vector<int> minOperations(string boxes) {
        int n = boxes.size();
        vector<int> operations(n, 0);
        int leftCount = (boxes[0] == '1') ? 1 : 0;
        int rightCount = 0;
        int totalOperations = 0;

        for (int i = 1; i < n; i++) {
            if (boxes[i] == '1') {
                rightCount++;
                totalOperations += i;
            }
        }

        operations[0] = totalOperations;
        int leftOperations = 0;

        for (int i = 1; i < n; i++) {
            totalOperations -= rightCount;
            leftOperations += leftCount;
            operations[i] = totalOperations + leftOperations;

            if (boxes[i] == '1') {
                leftCount++;
                rightCount--;
            }
        }

        return operations;
    }
};
```




```javascript []
var minOperations = function(boxes) {
    const n = boxes.length;
    const operations = new Array(n).fill(0);
    let leftCount = boxes[0] === "1" ? 1 : 0;
    let rightCount = 0;
    let totalOperations = 0;

    for (let i = 1; i < n; i++) {
        if (boxes[i] === "1") {
            rightCount++;
            totalOperations += i;
        }
    }

    operations[0] = totalOperations;
    let leftOperations = 0;

    for (let i = 1; i < n; i++) {
        totalOperations -= rightCount;
        leftOperations += leftCount;
        operations[i] = totalOperations + leftOperations;

        if (boxes[i] === "1") {
            leftCount++;
            rightCount--;
        }
    }

    return operations;
};
```




```go []
func minOperations(boxes string) []int {
    n := len(boxes)
    operations := make([]int, n)
    leftCount := 0
    if boxes[0] == '1' {
        leftCount = 1
    }
    rightCount := 0
    totalOperations := 0

    for i := 1; i < n; i++ {
        if boxes[i] == '1' {
            rightCount++
            totalOperations += i
        }
    }

    operations[0] = totalOperations
    leftOperations := 0

    for i := 1; i < n; i++ {
        totalOperations -= rightCount
        leftOperations += leftCount
        operations[i] = totalOperations + leftOperations

        if boxes[i] == '1' {
            leftCount++
            rightCount--
        }
    }

    return operations
}
```

---

![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style='width:250px'}
['Array', 'String', 'C', 'Prefix Sum', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'Dart']