C++ solution  |  O(n) |   expalined in details
\nThis can be a bit tricky especially for the beginners however, you can definitely understand the approach and solve it.\n\nAs per the question \'1\' represent
\nThis can be a bit tricky especially for the beginners however, you can definitely understand the approach and solve it.\n\nAs per the question \'**1**\' represent a box containing exactly one ball and \'**0**\' represent the empty box.\n\nThe Approach to solving this problem is **to traverse the string left to right** and **traverse the string left to right**. This is because the **left to right traversal** count the number of operations required to transfer ball in **right direction** to **ith** position and vice versa.\n\n### Left to right traversal \nNow the operation at **ith position** is calculated as the sum of  **total operation at (i-1)th (adjacent position) plus the number of balls seen so far during traversal**. This is because total operation necessary to transfer all balls to adjacent position is already calculated the only number of extra operation required is to transfer all the balls from adjacent to current position which is equal to **1 operation  X no. of ball seen**\n\n### Right to Left traversal \nPretty much the same thing like the left to right traversal.\n\n\n```\n vector<int> minOperations(string boxes) \n\t{\n\t\t int sum=0;\n\t\t int ballSeen =0;\n\t\t  vector<int> result(boxes.size(), 0);\n                \n            for( int i=1; i<boxes.size(); ++i )\n\t\t {\n\t\t\t if( boxes[i-1] ==\'1\')\n\t\t\t { \n\t\t\t\t ballSeen+=1;\n\t\t\t }\n\t\t\t sum+=ballSeen;\n\t\t\tresult[i]+=sum;\n\t\t }\n\n\t\t sum=0;\n\t\t ballSeen=0;\n\t\t for( int i=boxes.size()-2; i>=0; --i )\n\t\t {\n\t\t\t if( boxes[i+1] ==\'1\')\n\t\t\t { \n\t\t\t\t ballSeen+=1;\n\t\t\t }\n\t\t\t sum+=ballSeen;\n\t\t\t result[i]+=sum;\n\t\t }\n\t\t \n\t\treturn result;\n    }\n```\nIf this helped you in understanding the approach don\'t forget to upvote. :)
[]