easy C++ O(n) approach
\n\n# Approach\n1. Count the number of 1s in the array:\n\n2. Traverse the array to count the total number of 1s, denoted as k.\nIf k is 0 or equal to the lengt
\n\n# Approach\n1. Count the number of 1s in the array:\n\n2. Traverse the array to count the total number of 1s, denoted as k.\nIf k is 0 or equal to the length of the array (n), return 0 because no swaps are needed.\n3. Initialize the first window:\n\n4. Use a sliding window of size k to count the number of 0s in the first window (the first k elements of the array).\nThis count is stored in current_0s.\n5. Sliding window: Iterate through the array from the second element to the end, adjusting the window by one element at each step.\n6. For each new position of the window: Subtract the count of 0s for the element that is sliding out of the window (i.e., the element at index i-1). Add the count of 0s for the new element that is entering the window (i.e., the element at index (i + k - 1) % n to handle circularity).\n\n7. Update minimum count: Track and update the minimum number of 0s found in any window of size k.\n\nThis minimum number represents the minimum swaps needed since each 0 in the window corresponds to a necessary swap to group all 1s together.\n\n\nExample\nLet\'s walk through an example:\n\nGiven nums = [1, 0, 1, 0, 1, 0, 0, 1].\n\nCount the number of 1s:\n\nk = 4 (since there are four 1s in the array).\nInitialize the first window:\n\nThe first window of size k is [1, 0, 1, 0].\nNumber of 0s in this window is 2.\nSliding window:\n\nSlide the window one position to the right:\nNew window: [0, 1, 0, 1].\nAdjust current_0s by removing the element that slid out (1 at index 0) and adding the element that slid in (0 at index 4).\nNumber of 0s in this window is 2.\nContinue sliding:\nNext window: [1, 0, 1, 0], number of 0s is 2.\nNext window: [0, 1, 0, 0], number of 0s is 3.\nNext window: [1, 0, 0, 1], number of 0s is 2.\nNext window: [0, 0, 1, 1], number of 0s is 2.\nNext window: [0, 1, 1, 1], number of 0s is 1.\nUpdate minimum count:\n\nThe minimum number of 0s found in any window of size k is 1.\nHence, the minimum number of swaps needed to group all 1s together is \n\n# Complexity\n- Time complexity:\nO(n)\n\n\n# Code\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int n = nums.size();\n        int k = 0;\n\n        // Count the number of 1s in the array\n        for(int num : nums) {\n            if(num == 1) k++;\n        }\n\n        // If there are no 1s or the array is all 1s, no swaps needed\n        if(k == 0 || k == n) return 0;\n\n        // Initialize the number of 0s in the first window of size k\n        int current_0s = 0;\n        for(int i = 0; i < k; i++) {\n            if(nums[i] == 0) current_0s++;\n        }\n\n        int min_0s = current_0s;\n\n        // Use sliding window to find the minimum number of 0s in any window of size k\n        for(int i = 1; i < n; i++) {\n            // Remove the element that is sliding out of the window\n            if(nums[i - 1] == 0) current_0s--;\n            // Add the element that is sliding into the window\n            if(nums[(i + k - 1) % n] == 0) current_0s++;\n\n            // Update the minimum number of 0s found\n            if(current_0s < min_0s) min_0s = current_0s;\n        }\n\n        return min_0s;\n    }\n};\n\n```
['C++']