Group all 1s and 0s together, Sliding Window
Channel Link\n\n# Approach\nGroup all 1s together\nGroup all 0s together ( it will result in 1s placed at corners )\n\n# Complexity\n- Time complexity: O(n)\n A
## [Channel Link](https://www.youtube.com/channel/UCuxmikkhqbmBOUVxf-61hxw)\n\n# Approach\nGroup all 1s together\nGroup all 0s together ( it will result in 1s placed at corners )\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int countOfOnes = count(nums.begin(), nums.end(), 1);\n        int n = nums.size();\n        return min(solve(nums, n, 0, n - countOfOnes), solve(nums, n, 1, countOfOnes));\n    }\n\nprivate:\n    int solve(vector<int>& nums, int n, int g, int windowSize) {\n        if (windowSize == 0) return 0;\n        int swaps = 0;\n        for (int i = 0; i < windowSize; i++) {\n            if (nums[i] != g) swaps++;\n        }\n        int minSwaps = swaps;\n        for (int i = windowSize; i < n; i++) {\n            if (nums[i - windowSize] != g) swaps--;\n            if (nums[i] != g) swaps++;\n            minSwaps = min(minSwaps, swaps);\n        }\n        return minSwaps;\n    }\n};\n```\n```java []\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int countOfOnes = 0;\n        int n = nums.length;\n        for(int x: nums) if(x == 1) countOfOnes++;\n        return Math.min(\n            solve(nums, n, 0, n - countOfOnes), // group all 0s together\n            solve(nums, n, 1, countOfOnes) // group all 1s together\n        );\n    }\n\n    public int solve(int[] nums, int n, int g, int windowSize) {\n        if (windowSize == 0) return 0;\n        int swaps = 0;\n        for (int i = 0; i < windowSize; i++) {\n            if (nums[i] != g) swaps++;\n        }\n        int minSwaps = swaps;\n        for (int i = windowSize; i < n; i++) {\n            if (nums[i - windowSize] != g) swaps--;\n            if (nums[i] != g) swaps++;\n            minSwaps = Math.min(minSwaps, swaps);\n        }\n        return minSwaps;\n    }\n}\n```\n```Python3 []\nclass Solution:\n    def minSwaps(self, nums: List[int]) -> int:\n        countOfOnes = nums.count(1)\n        n = len(nums)\n\n        def solve(nums: List[int], n: int, g: int, windowSize: int) -> int:\n            if windowSize == 0:\n                return 0\n            \n            swaps = 0\n            for i in range(windowSize):\n                if nums[i] != g:\n                    swaps += 1\n            \n            minSwaps = swaps\n            for i in range(windowSize, n):\n                if nums[i - windowSize] != g:\n                    swaps -= 1\n                if nums[i] != g:\n                    swaps += 1\n                minSwaps = min(minSwaps, swaps)\n            \n            return minSwaps\n\n        return min(solve(nums, n, 0, n - countOfOnes), solve(nums, n, 1, countOfOnes))\n```
['Java']