Sliding Window | O(n) | C++ | Easy approach | Simple
Intuition\n Describe your first thoughts on how to solve this problem. \nI started thinking that, eventually, all the ones in the array will be grouped together
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI started thinking that, eventually, all the ones in the array will be grouped together to form a subarray whose size is equal to the total number of ```1\'s``` in the array. From this, I considered how to handle a subarray of that specific size.\n\nThis led me to the sliding window approach. By traversing the array with a fixed window size equal to the total number of ```1\'s```, I can calculate the maximum number of ```1\'s``` in each window. The answer will then be the number of ```0\'s``` in the subarray with the maximum number of ```1\'s```.\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```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int totalOnes= accumulate(nums.begin(),nums.end(),0);\n        int n= nums.size(),l=0,r=0,currOnes=0;\n        while(r<totalOnes){\n            if(nums[r]==1) currOnes++;\n            r++;\n        }\n        r=r%n;\n        int maxOnes=currOnes;\n        while(l<n){\n            if(nums[l]==1) currOnes--;\n            if(nums[r]==1) currOnes++;\n            maxOnes= max(maxOnes,currOnes);\n            r= (r+1)%n;\n            l++;\n        }\n        return totalOnes-maxOnes;\n    }\n};\n```
['C++']