C++/Java O(n) LTR + RTL
We first "move" balls from left to right, and track how many ops it takes for each box.\n\nFor that, we count how many balls we got so far in cnt, and accumulat
We first "move" balls from left to right, and track how many ops it takes for each box.\n\nFor that, we count how many balls we got so far in `cnt`, and accumulate it in `ops`.\n\nThen, we do the same from right to left. \n\n**C++**\n```cpp\nvector<int> minOperations(string boxes) {\n    vector<int> res(boxes.length()); \n    for (int i = 0, ops = 0, cnt = 0; i < boxes.length(); ++i) {\n       res[i] += ops;\n       cnt += boxes[i] == \'1\' ? 1 : 0;\n       ops += cnt;\n    }\n    for (int i = boxes.length() - 1, ops = 0, cnt = 0; i >= 0; --i) {\n        res[i] += ops;\n        cnt += boxes[i] == \'1\' ? 1 : 0;\n        ops += cnt;\n    }\n    return res;\n}\n```\n\n**Java**\n```java\npublic int[] minOperations(String boxes) {\n    int[] res = new int[boxes.length()];\n    for (int i = 0, ops = 0, cnt = 0; i < boxes.length(); ++i) {\n       res[i] += ops;\n       cnt += boxes.charAt(i) == \'1\' ? 1 : 0;\n       ops += cnt;\n    }    \n    for (int i = boxes.length() - 1, ops = 0, cnt = 0; i >= 0; --i) {\n        res[i] += ops;\n        cnt += boxes.charAt(i) == \'1\' ? 1 : 0;\n        ops += cnt;\n    }\n    return res;\n}\n```
[]