Beats 100% of users with C++
IntuitionThe problem involves calculating the minimum number of operations required to move all balls to each box. My first thought was to determine the cost of
# Intuition
The problem involves calculating the minimum number of operations required to move all balls to each box. My first thought was to determine the cost of moving balls to each box by considering the contributions from boxes to the left and right. A straightforward two-pass approach can efficiently compute these contributions.

# Approach
1. **Left-to-Right Pass**:
   - Traverse the string from left to right.
   - Keep track of the number of balls encountered so far (`count`).
   - For each box, calculate the total steps required to move all balls from the left to the current box and store it in the `operations` array.

2. **Right-to-Left Pass**:
   - Traverse the string from right to left.
   - Again, maintain a count of the balls encountered (`count`).
   - For each box, calculate the total steps required to move all balls from the right to the current box and add it to the existing value in the `operations` array.

This two-pass approach ensures that each box receives the minimum number of operations required to gather all balls.

# Complexity
- Time complexity:  
  $$O(n)$$  
  We traverse the string twice (left-to-right and right-to-left), so the time complexity is linear.

- Space complexity:  
  $$O(n)$$  
  An additional array of size \(n\) is used to store the results.

# Code
```cpp
class Solution {
public:
    vector<int> minOperations(string boxes) {
        int n = boxes.size();
        vector<int> operations(n, 0);
        
        // Left to right pass
        int count = 0, steps = 0;
        for (int i = 0; i < n; i++) {
            operations[i] += steps;
            count += (boxes[i] == '1');
            steps += count;
        }
        
        // Right to left pass
        count = 0; steps = 0;
        for (int i = n - 1; i >= 0; i--) {
            operations[i] += steps;
            count += (boxes[i] == '1');
            steps += count;
        }
        
        return operations;
    }
};

['C++']