Easy Solution || Beats 96.66% || Sliding Window || O(n) Time complexity || O(1) Space Complexity
Intuition\n\n#######   SLIDING WINDOW     ###########\nPLEASE upVOTE  \uD83D\uDE4F\uD83D\uDE4F\n\n\n# Approach\n\nApproach of this question is so simple, just w
# Intuition\n```\n#######   SLIDING WINDOW     ###########\nPLEASE upVOTE  \uD83D\uDE4F\uD83D\uDE4F\n```\n\n# Approach\n```\nApproach of this question is so simple, just we have to think more about circular array and subarrays.\nStep 1 : Declare a variable totalOnes and store total number of ones in it.\nStep 2 : Declare a variable currOnes then store the number of ones in it from 0th index till totalSum th index.(note that the window will be constant size of as same as totalSum value)\nStep 3 : Declare variable maxOnes and give it the value of currOnes for 1st window of size totalSum.\nStep 4 : Now at last to second step run loop from totalSum\'th index uptill (array size + totalOnes) and add the new value of further index of window and subtract the prevoius start index of previous window. And store the max number of ones into the maxOnes variable at each index.\nStep 5 : At the end of the day we have to return totalOnes-maxOnes for our ans.****\n```\n\n# Complexity\n- Time complexity:\n![image.png](https://assets.leetcode.com/users/images/9d8b7786-1e14-4d8e-bb55-f87940f4c04c_1722620124.8090777.png)\n\n\n- Space complexity:\n![image.png](https://assets.leetcode.com/users/images/36677266-ee7e-4cd7-9fa8-1c8653b06010_1722620157.904112.png)\n\n\n# Code\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int totalOnes = 0;\n        int currOnes = 0;\n        for(int i=0;i<nums.size();i++){\n            if(nums[i]==1){\n                totalOnes++;\n            }\n        }\n        for(int i=0;i<totalOnes;i++){\n            if(nums[i]==1){\n                currOnes++;\n            }\n        }\n        int maxOnes = currOnes;\n        for(int i=totalOnes;i<totalOnes+nums.size();i++){\n            currOnes = currOnes + nums[i%nums.size()] - nums[(i-totalOnes+nums.size())%nums.size()];\n            maxOnes = max(maxOnes, currOnes);\n        }\n        return totalOnes-maxOnes;\n    }\n};\n```
['Array', 'Sliding Window', 'C++']