(Easy understanding and code walk through) Minimum swaps to sort the array
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to group all the black balls (represented by \'1\') to the right side of th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to group all the black balls (represented by ```\'1\'```) to the right side of the string and all the white balls (represented by ```\'0\'```) to the left side. Swapping two adjacent balls is allowed, and we want to minimize the number of such swaps.\n\nSince we need to move the ```\'1\'```s (black balls) to the right, a natural way to think about the problem is to count how many ```\'0\'```s (white balls) each black ball has to "pass by" to reach its correct position. For each ```\'1\'``` encountered in the string, the number of swaps it requires is exactly the number of white balls to its right.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can approach the solution by iterating through the string in reverse (from right to left). This allows us to keep track of the number of white balls (```\'0\'```) to the right of each black ball (```\'1\'```).\n\n1. Initialize two variables:\n    - ```swaps``` to keep track of the total number of swaps.\n    - ```cnt``` to count the number of white balls (```\'0\'```s) encountered.\n2. Iterate through the string from right to left. For each character:\n    - If it\'s a white ball (```\'0\'```), increment the ```cnt``` since a white ball will need to be moved left.\n    - If it\'s a black ball (```\'1\'```), add the current count of white balls (```cnt```) to ```swaps``` because these white balls need to "pass by" the current black ball.\n3. Return the total ```swaps``` after processing the entire string.\n\n# Key Points:\n1. Reverse Iteration:\n    - The loop iterates over the string ```s``` from right to left (from the last index to the first). This is important because it allows us to handle all the ```1```s first as we want to move them to the right.\n2. Tracking Zeros (```cnt```):\n    - The variable ```cnt``` counts the number of ```\'0\'s``` encountered so far as you traverse the string from right to left. These ```\'0\'s``` represent white balls that need to be moved to the left.\n3. Swaps Calculation (```swaps```):\n    - Every time a ```\'1\'``` is encountered, the number of swaps needed to move that ```\'1\'``` to its correct position is exactly equal to the number of ```\'0\'s``` (```cnt```) seen so far. This is because each white ball (a ```\'0\'```) that is currently to the right of this black ball (a ```\'1\'```) needs to "pass by" the black ball in order for the black balls to group on the right side.\n4. Example Walkthrough:\n    - Suppose the input string is s = "01010".\n        - Start from the right (i = 4):\n            - s[4] = 0, so cnt = 1.\n        - Move left (i = 3):\n            - s[3] = 1, so swaps += cnt = 1 (there is 1 white ball to its right that will need to swap).\n        - Move left (i = 2):\n            - s[2] = 0, so cnt = 2.\n        - Move left (i = 1):\n            - s[1] = 1, so swaps += cnt = 2 (there are now 2 white balls to its right that need to swap).\n        - Move left (i = 0):\n            - s[0] = 0, so cnt = 3.\n    - The final value of swaps = 3.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity is $$O(n)$$, where is the length of the string. We are making a single pass over the string.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is $$O(1)$$ since we are only using a few variables to store the swap count and the number of white balls, independent of the input size.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long minimumSteps(string s) {\n        long long swaps = 0, cnt = 0;\n        for(int i=s.length()-1;i>=0;i--) {\n            if(s[i]==\'0\') ++cnt;\n            else swaps += cnt;\n        }\n        return swaps;\n    }\n};\n```
['Two Pointers', 'String', 'Greedy', 'C++']