Find positions of 1 +Prefix Sum||Beats 100%
IntuitionFind positions of 1.
then use prefix sum to count ans[i] for i
both C++ & Python are done.Approach
Initialize the vector ans of size n
Set the containe
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Find positions of 1.  
then use prefix sum to count `ans[i]` for `i`
both C++ & Python are done.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Initialize the vector `ans` of size `n`
2. Set the container `P` containing indices `i` for `1s` 
3. Using a loop to compute `a[0]+=i` if  `boxes[i]==1` for `i`
4. Set `pz=|P|`, L=0, R=pz  denote how many 1's on lefthand one / righthand side for index i
5. Use prefix sum to perform `for i=1 to n-1, with j=0`:
```
if (j<pz && i>P[j]) {
    L++;
    R--;
    j++;
}
ans[i]=ans[i-1]+L-R;
``` 
6. return `ans`
# 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 ||C++ prefix sum 0ms beats 100%
```cpp []
class Solution {
public:
    static vector<int> minOperations(string& boxes) {
        const int n = boxes.size();
        vector<int> ans(n, 0);
        vector<int> P;

        //positions of '1's
        for (int i = 0; i < n; i++) {
            if (boxes[i]=='1') {
                P.push_back(i);
                ans[0]+=i; //Initial cost for position 0
            }
        }

        int pz=P.size(), L=0, R=pz;

        // Calculate answer for the remaining indices
        for (int i=1, j= 0; i<n; i++) {
            if (j<pz && i>P[j]) {
                L++;
                R--;
                j++;
            }
            ans[i]=ans[i-1]+L-R;
        }

        return ans;
    }
};

```
```Python []
class Solution:
    def minOperations(self, boxes: str) -> List[int]:
        n=len(boxes)
        ans=[0]*n
        P=[]

        for i, x in enumerate(boxes):
            if x=='1':
                P.append(i)
                ans[0]+=i

        pz=len(P)
        L, R=0, pz
        j=0
        for i in range(1, n):
            if j<pz and i>P[j]:
                L+=1
                R-=1
                j+=1
            ans[i]=ans[i-1]+L-R
        return ans
```
['String', 'Prefix Sum', 'C++', 'Python3']