Eassyyy Pessyyy Solution ✅ | 98.67% Runtime 🔥 | TC = O(n) 🔥
Intuition
Instead of calculating the cost for each box individually (which would result in an inefficient O(n^2) solution), we can optimize the process using pr
# Intuition
* Instead of calculating the cost for each box individually (which would result in an inefficient O(n^2) solution), we can optimize the process using prefix sums:

1. Track the number of balls and the cumulative operations needed from left to right (prefix_left).
2. Track the same from right to left (prefix_right).
3. Combine both results to calculate the total operations for each box.
* This approach reduces redundant calculations and significantly improves efficiency.

# Approach
#### 1️⃣ Initialize Prefix Arrays:
* Create two prefix arrays: prefix_left and prefix_right.
* prefix_left[i]: Stores the total operations needed to move balls to box i from the left.
* prefix_right[i]: Stores the total operations needed to move balls to box i from the right.
#### 2️⃣ Populate prefix_left:
* Iterate from left to right.
* Keep track of ballsCount (number of balls encountered so far).
* For each box, update the cumulative operations needed from the left.
* Formula: `prefix_left[i]=prefix_left[i−1]+ballsCount`
#### 3️⃣ Populate prefix_right:
* Iterate from right to left.
* Keep track of ballsCount (number of balls encountered so far).
* For each box, update the cumulative operations needed from the right.
* Formula: `prefix_right[i]=prefix_right[i+1]+ballsCount`
#### 4️⃣ Combine Both Arrays:
* For each box i, the total operations are the sum of prefix_left[i] and prefix_right[i].
* Formula: `res[i]=prefix_left[i]+prefix_right[i]`
#### 5️⃣ Return Result:
* Return the res array containing the total operations for each box.
# Complexity
### Time Complexity:
* O(n) for calculating prefix_left.
* O(n) for calculating prefix_right.
* O(n) for combining both arrays.
* Overall: `O(n)`

### Space Complexity:

* Two prefix arrays of size n.
* Result array of size n.
* Overall: `O(n)`

# Code
```cpp []
class Solution {
public:
    vector<int> minOperations(string boxes) {
        int n = boxes.size();
        vector<int> prefix_left(n,0);
        vector<int> prefix_right(n,0);

        prefix_right[n-1] = 0;
        prefix_left[0] = 0;
        int ballsCount = 0;

        for(int i = 1; i < n; i++){
            ballsCount += (boxes[i-1] == '1')? 1:0;
            prefix_left[i] = prefix_left[i-1] + ballsCount;
        }
        ballsCount = 0;
        for(int i = n-2; i >= 0; i--){
            ballsCount += (boxes[i+1] == '1')? 1:0;
            prefix_right[i] = prefix_right[i+1] + ballsCount;
        }

        vector<int> res(n,0);
        for(int i = 0; i < n; i++){
            res[i] = prefix_left[i] + prefix_right[i];
        }
        return res;
    }
};
```
![Shinchan_upvote.jpg](https://assets.leetcode.com/users/images/8406fe9d-225c-4d74-bcb6-cf8cf0c531a0_1736151426.511234.jpeg)


['Array', 'String', 'Prefix Sum', 'C++']