Counting Ones || Sliding Window Approach || C++ Clean Code
Code: \n\n\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int cntOnes = 0;\n        int n = nums.size();\n        \n        for(auto
# Code: \n\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int cntOnes = 0;\n        int n = nums.size();\n        \n        for(auto& num : nums) if(num) cntOnes++;\n        \n        if(cntOnes == 0 || cntOnes == n) return 0;\n        \n        int maxOnes = 0, ones = 0;\n        int window = cntOnes;\n        \n        for(int i=0; i<window; i++) {\n            if(nums[i])\n                ones++;\n        }\n    \n        maxOnes = max(maxOnes, ones);\n        \n        for(int i=1; i<n; i++) {\n            if(nums[i - 1]) ones--;\n            if(nums[(i + window - 1) % n]) ones++;\n            maxOnes = max(maxOnes, ones);\n        }\n        \n        return cntOnes - maxOnes;\n    }\n};\n```\n\n**Complexity :** \n\n* Time : `O(N)` , N is length of `nums` array\n* Space : `O(1)\n\n***If you find this helpful, do give it a like :)***
['Sliding Window', 'Counting']