C++ | O(N) | DP
Approach - \nThe minimum number of moves to move all balls to box[i] will be the number of minimum moves to move balls to moves[i-1] + moves[i+1]\ni.e. box[i] =
**Approach** - \nThe minimum number of moves to move all balls to box[i] will be the number of minimum moves to move balls to moves[i-1] + moves[i+1]\ni.e. box[i] = moveLeft[i-1] + moveRight[i+1]\n\nTime Complexity - O(N)\nSpace Complexity - O(N)\n\n```\nclass Solution {\npublic:\n    vector<int> minOperations(string boxes) {\n        vector<int> left(boxes.size(), 0), right(boxes.size(), 0), ans(boxes.size(), 0);\n        int balls = 0;\n        \n        balls = boxes[0] - \'0\';\n        for(int i=1;i<boxes.size();i++){\n            left[i] = left[i-1] + balls;\n            balls += boxes[i] - \'0\';\n        }\n        \n        balls = boxes[boxes.size()-1] - \'0\';\n        for(int i=boxes.size()-2;i>=0;i--){\n            right[i] = right[i+1] + balls;\n            balls += boxes[i] - \'0\';\n        }\n        \n        for(int i=0;i<boxes.size();i++)\n            ans[i] = left[i] + right[i];\n        \n        return ans;\n    }\n};\n```
['Dynamic Programming', 'C']