Prefix Sum || Suffix Sum || Beats 100.00% || TC-O(n) || SC- O(n) 
IntuitionThe task involves determining the number of operations required to move all balls ('1') to each box. A naive approach would calculate this for every bo
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The task involves determining the number of operations required to move all balls ('1') to each box. A naive approach would calculate this for every box independently, resulting in $$O(n^2)$$ complexity. To optimize, we can leverage prefix and suffix calculations to keep track of the cumulative number of '1's and their indices, allowing efficient computation of operations at each step.  
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Initialization**:  
   - Compute the **total count of '1's** (`total_one_count`) and the **sum of their indices** (`total_one_index_sum`) in the string `boxes`. This will represent the global information about the positions of all balls.  

2. **Iterate Through Boxes**:  
   - Use two variables to maintain a **prefix count** (`one_till_now`) and the **prefix index sum** (`one_index_sum_till_now`) for the number of '1's encountered so far and their cumulative index sum.  

3. **Calculate Operations**:  
   - At each index `i`, calculate the operations required using the prefix and suffix information:  
     - Prefix contribution: `(one_till_now * i - one_index_sum_till_now)`  
     - Suffix contribution: `(suff_one_sum - i * suff_one)`  
       - Where `suff_one = total_one_count - one_till_now - (boxes[i] == '1')`  
       - And `suff_one_sum = total_one_index_sum - one_index_sum_till_now - ((boxes[i] == '1') ? i : 0)`  
   - Add the prefix and suffix contributions to get the total operations for the current box.  

4. **Update Prefix**:  
   - If the current box contains a '1', update the prefix variables `one_till_now` and `one_index_sum_till_now`.  

5. **Store Results**:  
   - Append the calculated operations to the result vector `ans`.  
# Complexity
- Time complexity: $$O(n)$$ 
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code
```cpp []
class Solution {
public:
    vector<int> minOperations(string boxes) {
        int n=boxes.length();
        int total_one_count=0, total_one_index_sum=0;
        for(int i=0; i<n; i++){
          if(boxes[i]=='1'){
            total_one_count++;
            total_one_index_sum+=i;
          }  
        }
        vector<int> ans;
        int one_till_now=0, one_index_sum_till_now=0;
        for(int i=0; i<n; i++){
            int suff_one= total_one_count-one_till_now-(boxes[i]=='1');
            int suff_one_sum= total_one_index_sum-one_index_sum_till_now-((boxes[i]=='1')?i:0);
            int number_of_operation= (one_till_now*i-one_index_sum_till_now)+(suff_one_sum-i*suff_one);
            ans.push_back(number_of_operation);
            if(boxes[i]=='1'){
            one_till_now++;
            one_index_sum_till_now+=i;
          } 
        }
        return ans;
    }
};
```
['Array', 'Math', 'String', 'Greedy', 'Suffix Array', 'Prefix Sum', 'C++']