2 Solutions | O(1) Space | O(N) Time | Similar to Minimum Swaps to Group All 1's Together
Count the minimum swaps required to group all ones in non circular Array. Question: https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together/\n2. Si
1. Count the minimum swaps required to group all ones in non circular Array. Question: https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together/\n2. Similarly, Count the minimum swaps required to group all zeros in non circular Array.\n3. Return minimum of both. (By seeing the example attached with code we can verify that the result will always either contains all 0s or 1s together in the array. \n4. To calculate 1st point. We will first find total ones in array. Then fix the size of window as total. Find the maximum no. of ones that can exist in any window of size \'total\' in array. Result = Total - max Size. Because we only need to swap \'Result\' number of values from that particular window.\n5. To calculate 2nd pointjust replace 1s with 0s.\n\n**Approach1:  Using Sliding Window O(1) Space, O(N) Time**\n```\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int op1 = minSwapsHelper(nums, 0);\n        int op2 = minSwapsHelper(nums, 1);\n        return Math.min(op1, op2);\n    }\n\t// Minimum swaps required to grup all \'val\' in non circular array.\n    public int minSwapsHelper(int[] data, int val) {\n        int len = data.length;\n        int total = 0;\n        for (int i = len - 1; i >= 0; i--) {\n            if (data[i] == val) total++;\n        }\n        if (total == 0 || total == len) return 0;\n        int i = 0, j = 0;\n        int maxWindowCount = 0, curWindowCount = 0;\n        while (j < total) {\n            if (data[j++] == val) curWindowCount++;\n        }\n        maxWindowCount = Math.max(maxWindowCount, curWindowCount);\n        while (j < len) {\n            if (data[i++] == val) curWindowCount--;\n            if (data[j++] == val) curWindowCount++;\n            maxWindowCount = Math.max(maxWindowCount, curWindowCount);\n        }\n        return total - maxWindowCount;\n    }\n}\n/*\n[0,1,1,1,0,0,1,1,0]\n\n111110000\n011111000\n001111100\n000111110\n000011111\n100001111\n110000111\n111000011\n111100001\n*/\n```\n\n**Approach2: Using Suffix Sum, O(N) Space, O(N) Time**\n```\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int op1 = minSwapsHelper(nums, 0);\n        int op2 = minSwapsHelper(nums, 1);\n        return Math.min(op1, op2);\n    }\n    public int minSwapsHelper(int[] data, int val) {\n        int len = data.length;\n        int[] right = new int[len + 1];\n        for (int i = len - 1; i >= 0; i--) {\n            right[i] = right[i + 1];\n            if (data[i] == (val^1)) right[i]++;\n        }\n        int total = right[0];\n        int curCount = 0;\n        int res = total - right[len - total];\n        for (int i = 0; i < total; i++) {\n            if (data[i] == (val^1)) curCount++;\n            int rem = (total - i - 1);\n            int ans = ((i + 1) - curCount) + (rem - right[len - rem]);\n            res = Math.min(res, ans);\n        }\n        return res;\n    }\n}\n/*\nFor Non Circular array,\n[1,0,1,0,1,0,0,1,1,0,1]\n 0 1 1 2 2 3 4 4 4 5 5\n 5 5 4 4 3 3 2 1 1 1 0\n \n11111100000 = 3\n01111110000 = 1 + 3\n00111111000 = 1 + 2\n00011111100 = 2 + 1\n00001111110 = 2 + 1\n00000111111 = 3\nmin = 3\n*/\n```
['Sliding Window', 'Prefix Sum']