Easier than EASY | C/C++ | JavaScript | Python
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(n)$$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$$O(n)$$
# Code
```cpp []
class Solution {
public:
    vector<int> minOperations(string boxes) {
        int left = 0, right = 0;
        int totalLeft = 0, totalRight = 0;
        vector<int> res;
        for (int i=0; i<boxes.size(); i++) {
            if(boxes[i] == '1') {
                totalRight += i;
                right ++;
            }
        }

        for (int i=0; i<boxes.size(); i++) {
            res.push_back(totalRight + totalLeft);
            if(boxes[i] == '1') {
                right--;
                left++;
            }
            totalLeft += left;
            totalRight-=right;
        }
        return res;
    }
};
```
```c []
int* minOperations(char* boxes, int* returnSize) {
    int left = 0, right = 0;
    int totalLeft = 0, totalRight = 0;
    int n = strlen(boxes);
    int *res = (int*)calloc(n, sizeof(int));

    for (int i=0; i<n; i++) {
        if(boxes[i] == '1') {
            totalRight += i;
            right ++;
        }
    }

    for (int i=0; i<n; i++) {
        res[i] += totalRight + totalLeft;
        if(boxes[i] == '1') {
            right--;
            left++;
        }
        totalLeft += left;
        totalRight-=right;
    }
    *returnSize = n;
    return res;
}
```
```javascript []
var minOperations = function(boxes) {
    let left = 0, right = 0;
    let totalLeft = 0, totalRight = 0;
    let res = [];
    for (let i=0; i<boxes.length; i++) {
        if(boxes[i] == '1') {
            totalRight += i;
            right ++;
        }
    }

    for (let i=0; i<boxes.length; i++) {
        res.push(totalRight + totalLeft);
        if(boxes[i] == '1') {
            right--;
            left++;
        }
        totalLeft += left;
        totalRight-=right;
    }
    return res;
};
```
```python []
class Solution:
    def minOperations(self, boxes: str) -> List[int]:
        left, right = 0, 0
        totalLeft, totalRight = 0, 0
        res = []
        for i in range(0, len(boxes)):
            if boxes[i] == '1':
                totalRight += i
                right += 1

        for i in range(0, len(boxes)):
            res.append(totalRight + totalLeft)
            if boxes[i] == '1':
                right -= 1
                left += 1
            totalLeft += left
            totalRight-=right
        return res
```
['Array', 'String', 'C', 'Prefix Sum', 'C++', 'Python3', 'JavaScript']