(With Proof)Easy Video explanation 🔥 || Greedy 🔥 || 2 Pointers 🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nTry to think in terms of indices and based on indexes come up with solution\n\n# Detail
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTry to think in terms of indices and based on indexes come up with solution\n\n# ***Detailed and easy Video Solution***\n\nhttps://youtu.be/cZNSHZKA4g8\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe provided solution calculates the minimum steps to group all black balls to the right and white balls to the left. It iterates through the binary string, recording the positions of \'0\'s (white) and \'1\'s (black). Then, it pairs these positions optimally, swapping and calculating the total distance traversed. The result is the minimum steps needed for the desired grouping.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(N)\n\n\n```C++ []\nclass Solution {\npublic:\n    long long minimumSteps(string s) {\n        int n=s.size();\n        vector<int>r,l;\n        for(int i=0;i<n;i++)\n        {\n            if(s[i]==\'0\')\n                r.push_back(i);\n            else\n                l.push_back(i);\n        }\n        int a=r.size(),b=l.size();\n        int i=0,j=a-1;\n        long long ans=0;\n        while(i<b && j>=0)\n        {\n            if(l[i]>r[j])\n                break;\n            ans=ans+abs(l[i]-r[j]);\n            i++;\n            j--;\n        }\n        return ans;\n    }\n};\n```\n```Python []\nclass Solution:\n    def minimum_steps(self, s):\n        n = len(s)\n        r = []\n        l = []\n        \n        for i in range(n):\n            if s[i] == \'0\':\n                r.append(i)\n            else:\n                l.append(i)\n\n        a = len(r)\n        b = len(l)\n        i = 0\n        j = a - 1\n        ans = 0\n\n        while i < b and j >= 0:\n            if l[i] > r[j]:\n                break\n            ans = ans + abs(l[i] - r[j])\n            i += 1\n            j -= 1\n\n        return ans\n\n```\n```Java []\nimport java.util.ArrayList;\n\nclass Solution {\n    public long minimumSteps(String s) {\n        int n = s.length();\n        ArrayList<Integer> r = new ArrayList<>();\n        ArrayList<Integer> l = new ArrayList<>();\n        \n        for (int i = 0; i < n; i++) {\n            if (s.charAt(i) == \'0\') {\n                r.add(i);\n            } else {\n                l.add(i);\n            }\n        }\n\n        int a = r.size();\n        int b = l.size();\n        int i = 0, j = a - 1;\n        long ans = 0;\n\n        while (i < b && j >= 0) {\n            if (l.get(i) > r.get(j)) {\n                break;\n            }\n            ans = ans + Math.abs(l.get(i) - r.get(j));\n            i++;\n            j--;\n        }\n\n        return ans;\n    }\n}\n```\n\n# ***If you like the solution Please Upvote and subscribe to my youtube channel***\n***It Motivates me to record more videos***\n\n*Thank you *\uD83D\uDE00\n\n\n
['Two Pointers', 'Greedy', 'Python', 'C++', 'Java']