C++ Simple and Clean Sliding-Window Solution, Detailed Explanation
Idea:\nWe want to find the "window" of size ones with the most ones.\nFirst, we count the ones in the original array.\nNow, because the array is circular, the e
**Idea:**\nWe want to find the "window" of size `ones` with the most ones.\nFirst, we count the ones in the original array.\nNow, because the array is circular, the easiest way is to concatenate two arrays and then we can search the window regularly.\nWe count the ones in the initial window in the left - from index 0 to `ones`.\nThen, in each iteration, we move the window one step to the right.\nIf the number we removed in the left is a one - we decrement `ones_in_window`.\nIf the number we added in the right is a one - we increment `ones_in_window`.\nWe keep track of the `mx`.\nOur result will be `ones - mx` because we need the number of **zeros** to swap in the window.\n\n**Time Complexity:** O(n)\n**Space Complexity:** O(n) (Because we are doubling the array)\n\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int ones = count(nums.begin(), nums.end(), 1), n = nums.size();\n        nums.insert(nums.end(), nums.begin(), nums.end());\n        \n        int ones_in_window = 0, mx = 0;\n        for (int i = 0; i < ones; i++)\n            ones_in_window += nums[i];\n\n        mx = ones_in_window;\n        \n        for (int i = ones; i < n + ones; i++) {\n            if (nums[i - ones] == 1) ones_in_window--;\n            if (nums[i] == 1) ones_in_window++;\n            mx = max(mx, ones_in_window);\n        }\n\n        return ones - mx;\n    }\n};\n```\n**Like it? please upvote!**
['C']