Cumulative Prefix Sum + Suffix Sum | Handwritten Dry Run|Beats 100%! 🚀 |✅✅✅
IntuitionCalculating Prefix Sum + Suffix Sum for . .  .
1. Number of ones till the current index.
2.Previous Contribution of all the propogated ones from back.A
![image.png](https://assets.leetcode.com/users/images/4310ac52-eb6a-4d59-892c-74270e77dff5_1736148208.8680801.png)

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Calculating Prefix Sum + Suffix Sum for . .  .
    1. Number of ones till the current index.
    2.Previous Contribution of all the propogated ones from back.

# Approach

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

1. **Initialize Variables:**  
   - Create two arrays, `prefix` and `suffix`, to store cumulative operation counts from the left and right directions, respectively.  
   - Use variables `x` to track cumulative contributions and `y` to count the number of '1's encountered.  

2. **Left-to-Right Pass:**  
   - Traverse the string from left to right.  
   - Update `x` by adding `y` (number of '1's encountered so far).  
   - Store the cumulative contribution in the `prefix` array.  
   - Increment `y` if the current box contains '1'.  

3. **Right-to-Left Pass:**  
   - Traverse the string from right to left.  
   - Update `x` by adding `y` similarly.  
   - Store the cumulative contribution in the `suffix` array.  
   - Increment `y` if the current box contains '1'.  

4. **Combine Results:**  
   - Calculate the total operations for each position by summing the values from `prefix` and `suffix`.  

5. **Return the Result:**  
   - Return the final computed operations stored in the `ans` array.  

# Dry Run
![WhatsApp Image 2025-01-06 at 12.22.12.jpeg](https://assets.leetcode.com/users/images/e4fec846-8b12-4628-be3a-c8125bcb29ae_1736147105.4221222.jpeg)

![WhatsApp Image 2025-01-06 at 12.22.13.jpeg](https://assets.leetcode.com/users/images/75fc54bc-1f5d-4510-a613-5aed36384dcf_1736147170.0406432.jpeg)


### **Time Complexity:**  
- **O(n):** Two passes (left-to-right and right-to-left) over the string.  

### **Space Complexity:**  
- **O(n):** Additional arrays `prefix`, `suffix`, and `ans` are used.  
# Code
```cpp []

class Solution {
public:
    vector<int> minOperations(string boxes) {
        int n = boxes.length();
        vector<int> prefix(n,0);
        vector<int> suffix(n,0);
        int x = 0;// previous contributions
        int y = 0;//number of ones
        // Calculating Prefix Sums
        for(int i = 0 ;i < n;i++){
            x += y;
            prefix[i] += x;
            if(boxes[i]=='1') y++;
        }
        for(int i = 0 ; i < n;i++)cout<<prefix[i]<<" ";
        x = 0;//previous contributions
        y = 0;//number of ones
        // Calculating Prefix Sums
        for(int i = n-1; i >= 0;i--){
            x+=y;
            suffix[i] += x;
            if(boxes[i]=='1') y++;
        }
        for(int i = n-1 ; i >=0 ;i--)cout<<suffix[i]<<" ";
        vector<int> ans(n,0);
        for(int i = 0 ; i < n;i++){    
             ans[i] =prefix[i] + suffix[i];
        }
        return ans;
    }
};
```
['Prefix Sum', 'C++']