Recursive Left and Right Pass using Arrays... Optimised version of Code
Initialization:
left and right arrays are initialized to store the cumulative costs.
cnt is used to keep track of the number of balls encountered so far.

Left
# Initialization:

> left and right arrays are initialized to store the cumulative costs.
> cnt is used to keep track of the number of balls encountered so far.

>Left Pass:

>Traverse the boxes from left to right.

> For each box, if the previous box contains a ball ('1'), increment the count (cnt).

> Update the left array with the cumulative cost of moving balls to the current box.

>Right Pass:

> Traverse the boxes from right to left.

> For each box, if the next box contains a ball ('1'), increment the count (cnt).

> Update the right array with the cumulative cost of moving balls to the current box.

> Combine Results:

> Combine the results from the left and right arrays to get the total minimum operations required for each box.
![image](https://media.makeameme.org/created/please-upvote-and.jpg)
# Complexity
- Time and Space complexities:
O(n)
# Code
```python3 []
class Solution:
    def minOperations(self, boxes: str) -> List[int]:
        n = len(boxes)
        left = [0] * n
        right = [0] * n
        cnt = 0
        for i in range(1, n):
            if boxes[i - 1] == '1':
                cnt += 1
            left[i] = left[i - 1] + cnt
        cnt = 0
        for i in range(n - 2, -1, -1):
            if boxes[i + 1] == '1':
                cnt += 1
            right[i] = right[i + 1] + cnt
        return [a + b for a, b in zip(left, right)]  
```
```Typescript []
function minOperations(boxes: string): number[] {
    const n = boxes.length;
    const ans = new Array(n).fill(0);
    for (let i = 1, count = 0; i < n; i++) {
        if (boxes[i - 1] === '1') {
            count++;
        }
        ans[i] = ans[i - 1] + count;
    }
    for (let i = n - 2, count = 0, sum = 0; i >= 0; i--) {
        if (boxes[i + 1] === '1') {
            count++;
        }
        sum += count;
        ans[i] += sum;
    }
    return ans;
}
```
```C []
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* minOperations(char* boxes, int* returnSize) {
    int n = strlen(boxes);
    int* ans = malloc(sizeof(int) * n);
    memset(ans, 0, sizeof(int) * n);
    for (int i = 1, count = 0; i < n; i++) {
        if (boxes[i - 1] == '1') {
            count++;
        }
        ans[i] = ans[i - 1] + count;
    }
    for (int i = n - 2, count = 0, sum = 0; i >= 0; i--) {
        if (boxes[i + 1] == '1') {
            count++;
        }
        sum += count;
        ans[i] += sum;
    }
    *returnSize = n;
    return ans;
}
```
``` JavaScript []
class Solution {
    public int[] minOperations(String boxes) {
        // Get the length of the input string
        int length = boxes.length();
        // Initialize the answer array with the same length
        int[] operations = new int[length];

        // Forward pass to calculate the operations required for each box from the left
        for (int i = 1, count = 0; i < length; ++i) {
            // If the previous box contains a ball, increment the count
            if (boxes.charAt(i - 1) == '1') {
                ++count;
            }
            // Accumulate the number of operations required for the current box
            operations[i] = operations[i - 1] + count;
        }
      
        // Backward pass to calculate the operations required for each box from the right
        for (int i = length - 2, count = 0, sum = 0; i >= 0; --i) {
            // If the next box contains a ball, increment the count
            if (boxes.charAt(i + 1) == '1') {
                ++count;
            }
            // Accumulate the number of operations required from the right side
            sum += count;
            // Add the right side operations to the current box's operations
            operations[i] += sum;
        }

        // Return the final array with minimum operations required for each box
        return operations;
    }
}
```

['Array', 'String', 'Recursion', 'C', 'Prefix Sum', 'C++', 'Java', 'TypeScript', 'Python3', 'JavaScript']