📌 Two-Pass Java Solution 🧩 | ⏱️ O(n) Time Complexity | ℹ️ Clear Explanation 
Intuition 💡The problem requires calculating the minimum number of operations needed to move all balls to each box in a given binary string. Instead of recalcula
# Intuition 💡
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires calculating the minimum number of operations needed to move all balls to each box in a given binary string. Instead of recalculating the operations for each box from scratch, we can leverage the following observations:

1. The total number of operations for each box can be split into contributions from:
   - **Balls to the left of the box**.
   - **Balls to the right of the box**.
2. Using this observation, we can compute the operations in two linear passes:
   - First, calculate contributions from the left side for each box.
   - Then, calculate contributions from the right side for each box and combine them.

By traversing the string twice (left-to-right and right-to-left), we efficiently calculate the minimum operations for each box.

# Approach 🛠️
<!-- Describe your approach to solving the problem. -->
1. **Variables Used**: 🔢
   - `ballsToLeft`: Tracks the total number of balls encountered during the left-to-right traversal.
   - `opsToLeft`: Tracks the cumulative operations required to move these balls to the current box.
   - `ballsToRight`: Tracks the total number of balls encountered during the right-to-left traversal.
   - `opsToRight`: Tracks the cumulative operations required to move these balls to the current box.
   - `answer`: An array to store the result for each box.

2. **Steps**: 🔄
   - **Left-to-Right Traversal**:
     - For each box `i`, calculate the contribution from balls to the left.
     - If a box contains a ball (`boxes[i] == '1'`), increment `ballsToLeft`.
     - Accumulate the operations (`opsToLeft`) needed to move all balls to the current box.
     - Store the result for each box in the `answer` array.
   - **Right-to-Left Traversal**:
     - For each box `i`, calculate the contribution from balls to the right.
     - If a box contains a ball (`boxes[i] == '1'`), increment `ballsToRight`.
     - Accumulate the operations (`opsToRight`) needed to move all balls to the current box.
     - Add the right-side contributions to the existing values in the `answer` array.

3. **Key Observations**: 🔍
   - The operations for the left and right sides are independent and can be computed separately.
   - The contributions for balls on either side can be efficiently tracked using cumulative sums.


# Complexity 📈
- Time complexity ⏱️ : $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity 💾 : $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code 💻
```java []
class Solution {
    public int[] minOperations(String boxes) {
        int n = boxes.length();
        int[] answer = new int[n];
        
        //Left -> ight traverse: Compute contributions from the left
        int ballsToLeft = 0, opsToLeft = 0;
        for (int i = 0; i < n; i++) {
            answer[i] = opsToLeft;
            ballsToLeft += boxes.charAt(i) - '0';
            opsToLeft += ballsToLeft;
        }

        // Right -> left traverse: Compute contributions from the right
        int ballsToRight = 0, opsToRight = 0;
        for (int i = n - 1; i >= 0; i--) {
            answer[i] += opsToRight;
            ballsToRight += boxes.charAt(i) - '0';
            opsToRight += ballsToRight;
        }
        
        return answer;
    }
}

```
['Array', 'Math', 'Two Pointers', 'Counting', 'Iterator', 'Prefix Sum', 'Java']