Easy || Beats 94.79% || Java. 
🙂 Guys i Urge you to solve on you own before the solution.IntuitionThe question is simply asking us for each box to give the number of moves to get all the ball
🙂 *Guys i Urge you to solve on you own before the solution.*

![image.png](https://assets.leetcode.com/users/images/35d372c8-ce06-47a7-ad3a-8324c704e965_1736143221.450752.png)

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The question is simply asking us for each box to give the number of moves to get all the balls to that box.
 
In simple terms for each box, the distace of other all one's present in the string.  

# Approach
<!-- Describe your approach to solving the problem. -->
The concept here is to find the distace's of ones at each position.

* We maintain the count of one's to our left as well as for right.
* In first iteration we find the sum of indexes of one's in the string 
so they can be counted as right ones.
* Here the presum related to left and sum is related to right ones
* Let's create the array to store the answer.
* Now we again iterate over the string and for each position we calculate the moves required. 
* By updating the sum's and one's count. 



# Complexity
- Time complexity:
It takes O(n+n) ~ O(n).
We are iterating first time to count rtones and sum -> n
Next we count the moves -> n

- Space complexity:
It takes O(1)
As the ans array we are returning it does not take part.

# Code
```java []
class Solution {
    public int[] minOperations(String boxes) {
        int n=boxes.length();
        int rtones=0, ltones=0;
        int sum=0,presum=0;
        // We are counting the number of ones and the sum of their indexes.
        for(int i=0;i<n;i++){
            if(boxes.charAt(i)=='1'){
                sum+=i; rtones+=1;
            }
        }
        // we iterate over boxes again to fill the respective moves.
        int[] res=new int[n];
        for(int i=0;i<n;i++){
            int moves = presum+sum;
            res[i]=moves;
            // we should update both the ones and the sum.
            if(boxes.charAt(i)=='1'){
                ltones += 1;
                rtones -= 1;
            }
            // As we are moving from left to right.
            presum += ltones;
            sum -= rtones;
        }
    return res;
    }
}
```
['Array', 'String', 'Prefix Sum', 'Java']