93% Beats in C++ || Easy understandable Sliding window code 💯✅
\n\n\n# Approach\n Describe your approach to solving the problem. \nWe can observe that after grouping the number of ones in the group is number of ones in the 
![image.png](https://assets.leetcode.com/users/images/cde72053-4933-4b5f-a5be-e4ad76a03b56_1722575630.5547433.png)\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can observe that after grouping the number of ones in the group is number of ones in the complete vector. now apply a sliding window and check for all subarrays of the size number of ones that how many ones are still required for making that subarray a group of all ones,So min of all that subarrays will give us the answer  \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        ios_base::sync_with_stdio(false);\n        cin.tie(NULL);\n        int n = nums.size();\n        int ones= 0;\n        for(int i=0;i<n;i++){\n            if(nums[i]==1) ones++;\n        }\n        int numones=0;\n        for(int i=0;i<ones;i++){\n            if(nums[i] == 1) {\n                numones++;\n            }\n        }\n        int ans = ones-numones;\n        for(int i=1;i<n;i++){\n            if(nums[i-1] == 1){\n                numones--;\n            }\n            if(nums[(ones+i-1)%n] ==1){\n                numones++;\n            }\n            ans = min(ans , ones-numones);\n        }\n        return ans;\n\n    }\n};\n```
['Array', 'Sliding Window', 'C++']