✅✅ two approach (brute force and prefix and suffix array)🔥🔥
Intuition:
The task is to compute the minimum operations required to move all balls to each box.
The number of operations for each box depends on the distance o
Intuition:
The task is to compute the minimum operations required to move all balls to each box.
The number of operations for each box depends on the distance of that box from other boxes containing balls.
Thus, we need to calculate the distance of each ball from every box and accumulate that value.
Approach:
Outer loop: Iterate over each box i.

Initialize x = 0 to store the total operations for moving balls to box i.
Inner loop: For each other box j:

If boxes[j] == '1' (box j contains a ball), compute the distance abs(i - j) and add it to x.
Store the computed value x for box i in the result vector.

After completing both loops, return the result vector.

Complexity:
Time complexity:
Outer loop: O(n)Inner loop: O(n)
Total complexity: O(n 2).
Space complexity:
The result vector v requires O(n) space.

# Code
```cpp []
class Solution {
public:
    vector<int> minOperations(string boxes) {
        vector<int> v(boxes.size(), 0); 
        
        for (int i = 0; i < boxes.size(); i++) {
            int x = 0;
            for (int j = 0; j < boxes.size(); j++) {
                if (boxes[j] == '1') { 
                    x += abs(i - j); 
                }
            }
            v[i] = x; 
        }

        return v;
    }
};

---

```


another approch with prefix and suffix vector


Intuition:
We need to compute the minimum operations required to move all balls to each box.
The operations for each box depend on the balls before it (on the left) and after it (on the right).
Prefix sum (left vector) calculates the total operations needed to move all balls to the left side of a box.
Suffix sum (right vector) calculates the total operations needed to move all balls to the right side of a box.
By calculating both the left and right distances for each box, we can determine the total number of operations needed.


Approach:
Left vector (left[i]):
This stores the total number of operations needed to move all balls from the left side to box i.
Initialize left[0] to 0 and maintain a count variable to track how many balls are on the left.
For each box i, update left[i] as the sum of operations for the previous box plus the count of balls encountered.
Update count to include the ball at the current box if boxes[i] == '1'.
Right vector (right[i]):

This stores the total number of operations needed to move all balls from the right side to box i.
Start from the last box and move to the left. Use a similar strategy to update the right vector.
Initialize right[n-1] to 0 and maintain a count variable for balls on the right side.
For each box i, update right[i] as the sum of operations for the next box plus the count of balls encountered.
Update count to include the ball at the current box if boxes[i] == '1'.
Combine the results:

The result for each box i is the sum of left[i] and right[i], which gives the total operations needed to move all balls to box i.
Return the final result.

Complexity:
Time complexity:
Calculating the left vector takes O(n).
Calculating the right vector takes O(n).
Combining the results also takes O(n).
Total time complexity: O(n).
Space complexity:
We use 3 vectors: left, right, and result, each of size n.
Total space complexity: O(n).




# Code
```cpp []
class Solution {
public:
    vector<int> minOperations(string boxes) {
        int n = boxes.size();
        vector<int> left(n, 0);
        int count = (boxes[0] == '1') ? 1 : 0;  


        for (int i = 1; i < n; i++) {
            left[i] = left[i - 1] + count;  
            count += (boxes[i] == '1');    
        }

        count = (boxes[n - 1] == '1') ? 1 : 0;  
        vector<int> right(n, 0);

 
        for (int i = n - 2; i >= 0; i--) {
            right[i] = right[i + 1] + count;   
            count += (boxes[i] == '1');       
        }

      
        vector<int> result(n, 0);
        for (int i = 0; i < n; i++) {
            result[i] = left[i] + right[i];
        }

        return result;
    }
};


---

```
['Prefix Sum', 'C++']