Easy understanding C++ code with approach
We are given a binary circular array (0s and 1s) and we are required to group all the 1s together with minimum no. of swaps.\n\nFirst we count the total no. of 
We are given a binary circular array (0s and 1s) and we are required to group all the 1s together with minimum no. of swaps.\n\nFirst we count the total no. of 1s in the given array. If the no. of 1s in the array is less than or equal to 1, this means all the 1s are grouped together, so no swaps are required and we return 0.\n\nWe are required to minimise the no. of swaps, so we find the ***sub array having maximum no. of ones in all sub arrays with length equal to total no. of ones.*** This makes the no. of 0s in the sub array low and swaps are reduced. \nSo now the answer is no. of 0s present in the subarray we found as we have to swap those 0s with the remaining 1s. i.e, the answer is **no. of zeroes in the sub array found = (total length of sub array - ones in that sub array).**\nFor finding the sub array having maximum ones of length k equal to total no. of ones , it looks like a ***fixed length sliding window problem*** which could be solved in O(n) time.\n\nBut now the question also mentions a specific point that it is a **circular array**. So the last element in the given array is followed with the first element. So we have n sub arrays for any length (n represents no. of elements in the given array). So while sliding the window, now we iterate upto (n + sub array length) to cover all sub arrays in the circular pattern.\n\n**Time Complexity:** O(n), **Space Complexity:** O(1)\n\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int n = nums.size();\n        int ones = 0; // total no. of ones\n        for(int i=0;i<n;i++){\n            if(nums[i]==1) ones++;\n        }\n        if(ones<=1) return 0;\n        \n        int k = ones; // sliding window length\n        int maxOnes = 0; // maxOnes in sub array of length k \n        int cnt = 0;\n        for(int i=0;i<k;i++){\n            if(nums[i]==1) cnt++;\n        }\n        maxOnes = cnt;\n        for(int i=k;i<n+k;i++){\n            if(nums[i-k]==1) cnt--; // if element removing from window is 1, then decrease count.\n            if(nums[i%n]==1) cnt++; // if element adding to window is 1, then increase count.\n            maxOnes = max(maxOnes,cnt); // maintaing maxOnes for all sub arrays of length k.\n        }\n        return(k - maxOnes); // (total length of subarray - ones in the sub array found) \n    }\n};\n```\n\n***Upvote if it helps.***
['C', 'Sliding Window', 'C++']