Easy Java Solution beats 💯✅✅
IntuitionThe task involves calculating the minimum number of operations required to move all balls to each box in a given string. Each character in the string (
## Intuition
The task involves calculating the minimum number of operations required to move all balls to each box in a given string. Each character in the string (`'1'` or `'0'`) represents whether a box contains a ball (`'1'`) or not (`'0'`). The challenge is to efficiently compute these operations for every box.

---

## Approach
1. **Prefix and Suffix Arrays**:
   - Use two arrays (`prefix` and `suffix`) to calculate cumulative operations from the left and right directions, respectively.
   - `prefix[i]`: Total operations needed to move balls from boxes `0` to `i`.
   - `suffix[i]`: Total operations needed to move balls from boxes `i` to the last box.

2. **Iterate Through Boxes**:
   - Populate `prefix` and `suffix` arrays using the number of balls encountered (`sump` and `sums`) as you move through the string.

3. **Combine Results**:
   - For each box, add `prefix[i]` and `suffix[i]` to get the total operations for that box.

---

## Complexity
- **Time Complexity**:  
  $$O(n)$$  
  The algorithm iterates through the string twice (once for prefix and once for suffix), which is linear.

- **Space Complexity**:  
  $$O(n)$$  
  Two auxiliary arrays (`prefix` and `suffix`) of size `n` are used.

---

## Code with Comments

```java
class Solution {
    public int[] minOperations(String boxes) {
        int n = boxes.length();
        int[] prefix = new int[n];
        int[] suffix = new int[n];
        int sump, sums;

        // Initialize counters for balls at both ends
        sump = (boxes.charAt(0) == '1') ? 1 : 0;
        sums = (boxes.charAt(n - 1) == '1') ? 1 : 0;

        // Calculate prefix array (left to right)
        for (int i = 1; i < n; i++) {
            prefix[i] = prefix[i - 1] + sump;
            if (boxes.charAt(i) == '1') {
                sump++; // Increment number of balls
            }
        }

        // Calculate suffix array (right to left)
        for (int i = n - 2; i >= 0; i--) {
            suffix[i] = suffix[i + 1] + sums;
            if (boxes.charAt(i) == '1') {
                sums++; // Increment number of balls
            }
        }

        // Combine prefix and suffix arrays
        for (int i = 0; i < n; i++) {
            prefix[i] += suffix[i];
        }

        return prefix; // Result is stored in prefix
    }
}
```

---

### Example Walkthrough

**Input**: `"110"`  
- **Prefix Array Calculation**:
  - At `i=1`: Add operations from left; balls so far: `1`.
  - At `i=2`: Add operations from left; balls so far: `2`.
- **Suffix Array Calculation**:
  - At `i=1`: Add operations from right; balls so far: `1`.
  - At `i=0`: Add operations from right; balls so far: `2`.
- **Combine Prefix and Suffix**:  
  Result: `[1, 1, 3]`.

**Output**: `[1, 1, 3]`


['Java']