%99.69 Faster Solution with C++
Intuition\nThe problem asks us to reach a string looking like "0...0011...1", where 0 represents white balls and 1 represents black balls.\n\nLet\'s look at the
# Intuition\nThe problem asks us to reach a string looking like "0...0011...1", where 0 represents white balls and 1 represents black balls.\n\nLet\'s look at the sum of indices of white balls (0s). For the most minimum number of steps, we would NEVER make a swap between two white balls or two black balls. If we make a swap, it has to be between a white ball and a black ball, which must move the white ball to the left (index of that ball is decremented by one) and the black ball to the right (index of that ball is incremented by one). \n\nHence, for every swap operation, the sum of indices of white balls is decremented by one. Let\'s use this intuition in our solution.\n\n# Approach\nFirst of all, we traverse the array once to retrieve the number of white balls and the sum of their indices. At the end, all white balls have to be laid on the lefthand side, starting from 0 to (m - 1), where m is the number of white balls. \n\n$$1 + 2 + .... + (n - 1) + n = n * (n + 1) / 2$$\n\nHence, $$0 + 1 + .... + (m - 2) + (m - 1) = (m - 1) * m / 2$$.\n\nSince the sum decreases by one in each valid swap operation, the difference between the initial sum of indices and the number above gives us the minimum number of swap operations.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long minimumSteps(string s) {\n        long long zero_indices = 0;\n        long long zero_count = 0;\n\n        for (size_t i = 0; i < s.size(); i++) {\n            if (s[i] == \'0\') {\n                zero_count++;\n                zero_indices += static_cast<long long>(i);\n            }\n        }\n\n        return zero_indices - ((zero_count * (zero_count - 1)) / 2);\n    }\n};\n```
['Math', 'C++']