Simple C++ solution | Sliding Window
Intuition\nSince we need to find the minimum swaps, we need to count the zeros in a fixed window of size equal to the total number of ones.\n\n# Approach\n1. Co
# Intuition\nSince we need to find the minimum swaps, we need to count the zeros in a fixed window of size equal to the total number of ones.\n\n# Approach\n1. Count the number of ones which will be the size of the sliding window.\n2. Append the array to itself at the end as it is a circular array.\n3. Count the number of zeros in the window.\n4. Move the sliding window till the end and find the minimum count of the zeros.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int m = nums.size();\n        int ones = 0;\n        for(int i=0;i<m;++i){if(nums[i]==1){++ones;}}\n        if(ones<=1){return 0;}\n        for(int i=0;i<m;++i){nums.push_back(nums[i]);}\n        m=nums.size();\n        int left = 0;int right = ones-1;int swaps = 0;\n        for(int i=left;i<=right;++i){if(nums[i]==0){++swaps;}}\n        int ans = swaps;\n        while(right<m-1){\n            if(nums[++right]==0){++swaps;}\n            if(nums[left++]==0){--swaps;}\n            ans=min(ans,swaps);\n        }\n        return ans;\n    }\n};\n```\n\nPlease upvote if you found it useful!!
['Array', 'Sliding Window', 'C++']