C++ || PYTHON || Sliding Window (approach) || Easy to understand || O(1) space
Intuition - It\'s a circular array problem, but to handle circular array contraint just append the array to itself or use modulo for this purpose and it will be
**Intuition - It\'s a circular array problem, but to handle circular array contraint just append the array to itself or use modulo for this purpose and it will become a linear array type problem.**\n\n**-** find the total number of 1\'s in whole array. like **totalOnes** in the below code.\n**-** count of number of 1\'s in window of constant size **totalOnes** and keep moving this window by one index to get maximum count of 1\'s in all windows.\n**-** in the window with maximum of number of 1\'s, we will swap all of its 0\'s with other 1\'s which are not in this window, so our anser will be **(totalOnes - maxOnesInWindow)**.\n**-** When i >= n we will use modulo to make it work like a circular array.\n# **C++**\n```\nclass Solution{\npublic:\n    int minSwaps(vector<int> &nums){\n        int totalOnes = 0, n = nums.size();\n        //here totalOnes is the number of 1\'s present in array\n        for (auto it : nums)\n            if (it == 1) totalOnes++;\n\t\t\n        //now we will count the maximum number of 1\'s in any window of size totalOnes\n        int maxOnesInWindow = 0, onesInCurrWindow = 0, i = 0;\n        for (i = 0; i < totalOnes; i++)\n            if (nums[i] == 1) maxOnesInWindow++;\n        \n        //onesInCurrWindow is the count of 1\'s in the current window\n\t\tonesInCurrWindow = maxOnesInWindow;\n\t\t\n        //Now we will move the array with a constant window size of totalOnes\n        for (; i < n + totalOnes; i++){\n            //In this step we are moving window forward by one step\n            //if nums[i%n] is 1 then add 1 to onesInCurrWindow\n            //if nums[i - totalOnes] is 1 then subtrct 1 from onesInCurrWindow\n            onesInCurrWindow += (nums[i % n] - nums[i - totalOnes]);\n            maxOnesInWindow = max(onesInCurrWindow, maxOnesInWindow);\n        }\n        return totalOnes - maxOnesInWindow;\n    }\n};\n```\n# **PYTHON**\n```\nclass Solution:\n    def minSwaps(self, nums: List[int]) -> int:\n\t    #here totalOnes is the number of 1\'s present in array\n        totalOnes = sum(i == 1 for i in nums)\n\t\t\n\t\t#onesInCurrWindow is the count of 1\'s in the current window\n\t\tonesInCurrWindow = sum(i == 1 for i in nums[:totalOnes])\n        \n\t\t#now we will count the maximum number of 1\'s in any window of size totalOnes\n\t\tmaxOnesInWindow = onesInCurrWindow\n\t\t\n        #Now we will move the array with a constant window size of totalOnes\n        for i in range(totalOnes, len(nums) + totalOnes):\n\t\t    #In this step we are moving window forward by one step\n            #if nums[i%n] is 1 then add 1 to onesInCurrWindow\n            #if nums[i - totalOnes] is 1 then subtrct 1 from onesInCurrWindow\n            onesInCurrWindow += (nums[i % len(nums)] - nums[i - totalOnes]);\n            maxOnesInWindow = max(maxOnesInWindow, onesInCurrWindow)\n            \n        return totalOnes - maxOnesInWindow\n```\n\n**Time complexity :- O(n)\nSpace complexity :- O(1)**\n\n**Question - why we are traversing only upto i < (n + totalOnes) ?**\n**Answer** - because our window size is totalOnes so, the last window we need to check will be (last one element + (totalOnes-1) elements from starting)....\nand we are using i as rightmost index of every window, so the largest value i will attain inside for loop is (n+totalOne-1).\n**Question - What is logic of this line ```onesInCurrWindow += (nums[i % n] - nums[i - totalOnes])``` or why are we adding when ```nums[i%n]==1``` and subtracting when ```nums[i-totalOnes]==1``` ?**\n**Answer** - actually here in each iteration we are moving our window of constant size totalOnes by one index, and counting number of 1\'s in that new window....\ntake this example num = [1, 0, 0, 1, 0, 1, 0 , 1]\nhere totalOnes = 4,\n\nwhen i = 3, our window is [nums[0], nums[1], nums[2], nums[3]] or [1,0,0,1] and number of 1\'s in this window is 2. so, onesInCurrWindow = 2.\n\nnow move forward by one index ,\ni = 4, and now our current window will be [nums[1], nums[2], nums[3], nums[4]]....\nnow what\'s the difference in this new window and previous window,,, you will see that the first element of previous window(which is nums[i-totalOnes] or nums[0]) is removed and we added a new element(which is nums[i]) and this is new window.\nso, how to update the value of onesInCurrWindow, as said above we have removed nums[i-totalValues] so, check whether that nums[i-totalOnes] == 1, if yes then subtract 1 from onesInCurrWindow because that element is no longer part of our current window hence we need to remove it\'s 1 count from onesInCurrWindow,,, and we have added a new element nums[i] in current window so, check whether nums[i] is 1 or not, if it is 1 then add add +1 in onesInCurrWindow.\n\n**If you find this useful please UPVOTE, or have any suggection then please share it in comments**
['Sliding Window']