C++ solution O(n)
The approach used:\nWork done to move the balls to any box is equal to work done to bring the balls in previous box plus the number of balls in the previous box
The approach used:\n**Work done to move the balls to any box is equal to work done to bring the balls in previous box plus the number of balls in the previous box**\n\n``` \nclass Solution {\npublic:\n    vector<int> minOperations(string s) {\n        int l = s.size();\n        int w1[l],w2[l];\n        vector<int>ans;\n        ans.resize(l);\n        w1[0]=0;w2[l-1]=0;\n        int balls =0;\n        for(int i=1;i<l;i++){\n            if(s[i-1]==\'1\') balls++;\n            w1[i]=w1[i-1]+balls;\n        }\n        balls=0;\n        for(int i=l-2;i>=0;i--){\n            if(s[i+1]==\'1\') balls++;\n            w2[i]=w2[i+1]+balls;\n        }\n        for(int i=0;i<l;i++){\n            ans[i]=w1[i]+w2[i];\n        }\n        return ans;\n    }\n};\n```\n
[]