Simple and Easy Solution | ✅Beats 100% | C++| Java | Python | JavaScript
⬆️Upvote if it helps ⬆️Connect with me on Linkedin [Bijoy Sing]IntuitionThe problem requires finding the minimum moves needed to move all balls to each position
# ⬆️Upvote if it helps ⬆️

---

## Connect with me on Linkedin [Bijoy Sing]

---

```cpp []
class Solution {
public:
    vector<int> minOperations(string boxes) {
        vector<int> pos, ans;
        int len = boxes.size();
        
        for(int i = 0; i < len; i++)
            if(boxes[i] == '1') 
                pos.push_back(i);
        
        for(int i = 0; i < len; i++) {
            int sum = 0;
            for(int idx : pos) {
                int dst = abs(i - idx);
                sum += dst;
            }
            ans.push_back(sum);
        }
        return ans;
    }
};
```
```python []
class Solution:
    def minOperations(self, boxes: str) -> List[int]:
        pos, ans = [], []
        ln = len(boxes)
        
        for i in range(ln):
            if boxes[i] == '1':
                pos.append(i)
                
        for i in range(ln):
            sm = 0
            for idx in pos:
                dst = abs(i - idx)
                sm += dst
            ans.append(sm)
        return ans
```
```java []
class Solution {
    public int[] minOperations(String boxes) {
        List<Integer> pos = new ArrayList<>();
        int len = boxes.length();
        int[] ans = new int[len];
        
        for(int i = 0; i < len; i++)
            if(boxes.charAt(i) == '1')
                pos.add(i);
        
        for(int i = 0; i < len; i++) {
            int sum = 0;
            for(int idx : pos) {
                int dst = Math.abs(i - idx);
                sum += dst;
            }
            ans[i] = sum;
        }
        return ans;
    }
}
```
```javascript []
class Solution {
    minOperations(boxes) {
        let pos = [], ans = [];
        let len = boxes.length;
        
        for(let i = 0; i < len; i++) 
            if(boxes[i] === '1')
                pos.push(i);
        
        for(let i = 0; i < len; i++) {
            let sum = 0;
            for(let idx of pos) {
                let dst = Math.abs(i - idx);
                sum += dst;
            }
            ans.push(sum);
        }
        return ans;
    }
}
```
# Intuition
The problem requires finding the minimum moves needed to move all balls to each position. We can solve this by first tracking all ball positions and then calculating distances from each position to all balls.

# Approach
1. First store all positions where balls are present (where boxes[i] == '1')
2. For each position i in boxes:
   - Calculate sum of absolute differences between current position and all ball positions
   - This sum represents minimum moves needed to move all balls to position i

# Complexity
- Time complexity: $$O(n * k)$$
  where n is length of string and k is number of balls
- Space complexity: $$O(k)$$
  where k is number of balls (storing ball positions)

### *If you have any questions or need further clarification, feel free to drop a comment! 😊*
['Array', 'String', 'Prefix Sum', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']