Easy approach for beginner || Prefix Sum Algorithm
IntuitionThe problem requires calculating the minimum number of moves to bring all balls to each box. My first thought was to determine the positions of the bal
# Intuition
The problem requires calculating the minimum number of moves to bring all balls to each box. My first thought was to determine the positions of the balls and then compute the cumulative operations required for each box. Instead of recalculating the operations from scratch for every box, we can use a cumulative approach to optimize the calculation.

# Approach
1. Identify the positions of all boxes containing balls ('1') and store these indices in an array.
2. Calculate the total distance from all balls to the first box. This serves as the base case for further calculations.
3. Use a cumulative difference (`deltaDistance`) to update the operations required as we move from one box to the next.
   - If the current box is adjacent to a box with a ball, adjust `deltaDistance` accordingly.
   - Update the total distance for the current box using the previously computed distance and `deltaDistance`.
4. Store the result for each box in an output array.

This approach ensures we only loop through the array a few times, making it efficient.

# Complexity
- Time complexity:  
  $$O(n)$$  
  We loop through the `boxes` array twice:
  - Once to identify positions of balls.
  - Once to calculate cumulative operations for each box.

- Space complexity:  
  $$O(n)$$  
  Space is used for the `operations`, `hasBallAfter`, and `ballIndices` arrays.

# Code
```javascript
/**
 * @param {string} boxes
 * @return {number[]}
 */
var minOperations = function(boxes) {
    const operations = Array(boxes.length).fill(0)
    const hasBallAfter = Array(boxes.length).fill(false)
    const ballIndices = []

    for (let i = 0; i < boxes.length; i++) {
        if (boxes[i] === '1') {
            ballIndices.push(i)
            if (i + 1 < boxes.length) hasBallAfter[i + 1] = true
        }
    }

    let totalDistance = ballIndices.reduce((acc, cur) => acc + cur, 0)
    operations[0] = totalDistance
    let deltaDistance = -ballIndices.length 

    for (let i = 1; i < boxes.length; i++) {
        if (hasBallAfter[i]) deltaDistance += 2
        totalDistance += deltaDistance
        operations[i] = totalDistance
    }

    return operations
};
```
['Array', 'Prefix Sum', 'JavaScript']