C++ || Prefix Sum
Approach:\n1- We will calculate the number of 1\'s in the array.\n2- We will create Prefix sum array.\n3- Since, we know we can swap any distinct elements of an
Approach:\n1- We will calculate the number of 1\'s in the array.\n2- We will create Prefix sum array.\n3- Since, we know we can swap any distinct elements of any indices. Example : [1,0,1,0,0,1] . Here if we swap the 1 index with the last index. array becomes [1,1,1,0,0,0]. So we know that that we need to check the required swaps if we substract the total ones with the number of one\'s in that window. Example- if the array is [1,0,0,1,1,1,1,0,0,1],\nwe will check the number of one\'s in the window of total ones. Say total ones are- 6. Size of array 10\nSo we will check in the window of six. First window is 1,0,0,1,1,1. So, number of ones in this window is 4 and total ones are 6. so required swaps in this window is 6-4=2.\nAs in the question it is mentioned that array is **circular** so if the index in the array is such that index+count>=size of array. We can actually mod the index. Lets say we are at the last index of array. window is [1] and also the mod is (9+6-1)%10 = 4. So we will consider index 9,0,1,2,3,4.\nSo we will simply count the minimum among the ans.\n\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int maxi = INT_MAX;\n        int c=(nums[0]==1)?1:0;\n        for(int i=1;i<nums.size();i++){\n            if(nums[i]==1) c++;\n             nums[i]+=nums[i-1];   \n        }\n        for(int i=0;i<nums.size();i++){\n            int lastSum = (i-1>=0)?nums[i-1]:0;\n            int nextSum = (i+c-1<nums.size())?nums[i+c-1]:(nums[nums.size()-1]+nums[(i+c-1)%nums.size()]);\n            maxi = min(maxi,c-(nextSum-lastSum));\n        }\n        return maxi;\n    }\n};\n```
[]