Best Solution for Arrays, Prefix Sum 🚀 in C++, Python and Java || 100% working
Intuition💡 The problem involves determining the minimum number of moves to bring all balls in the boxes to each box. It requires moving balls step-by-step while
# Intuition
💡 The problem involves determining the minimum number of moves to bring all balls in the boxes to each box. It requires moving balls step-by-step while keeping track of cumulative moves.

# Approach
👉 Traverse the boxes twice:
1. **Left to right**: Calculate moves while accumulating counts of balls.
2. **Right to left**: Update moves with the same logic but in reverse.

This ensures we calculate the moves efficiently in two sweeps.

# Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(n)$$

# Code
```cpp []
class Solution {
public:
    vector<int> minOperations(string boxes) {
        int n = boxes.size();
        vector<int> ans(n,0);
        int c = 0, m = 0;
        for(int i=0;i<n;i++){
            ans[i] += m;
            if(boxes[i] == '1') c++;
            m += c;
        }
        c=0,m=0;
        for(int i = n-1;i>=0;i--){
            ans[i] += m;
            if(boxes[i] == '1') c++;
            m += c;
        }
        return ans;
    }
};
```

```python []
class Solution:
    def minOperations(self, boxes: str) -> List[int]:
        n = len(boxes)
        ans = [0] * n
        c = m = 0
        for i in range(n):
            ans[i] += m
            if boxes[i] == '1':
                c += 1
            m += c
        c = m = 0
        for i in range(n - 1, -1, -1):
            ans[i] += m
            if boxes[i] == '1':
                c += 1
            m += c
        return ans
```

```java []
class Solution {
    public int[] minOperations(String boxes) {
        int n = boxes.length();
        int[] ans = new int[n];
        int c = 0, m = 0;
        for (int i = 0; i < n; i++) {
            ans[i] += m;
            if (boxes.charAt(i) == '1') c++;
            m += c;
        }
        c = m = 0;
        for (int i = n - 1; i >= 0; i--) {
            ans[i] += m;
            if (boxes.charAt(i) == '1') c++;
            m += c;
        }
        return ans;
    }
}
```
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">

# Connect with me on LinkedIn for more insights! 🌟 Link in bio
['Array', 'String', 'Prefix Sum', 'Python', 'C++', 'Java', 'Python3']