Sliding Window + Comments
We just want to get all the 1\'s together irrespective of the places where we put them, we just want them together.\n\n Maintain a window of size of number of o
We just want to get all the 1\'s together irrespective of the places where we put them, we just want them together.\n\n* Maintain a window of size of number of ones in whole array.\n* Now check in every consecutive window of same size,keep on counting number of zeroes present.\n* Number of Zeroes present in the current window will be the minimum replacement swap.\n\n# Twist: \nThe window needs to run in a circular manner.\n`r=(r+1)%n`  -> For getting circular window.\n\n#   C++: \n   \n      int minSwaps(vector<int>& nums){     \n        int n=size(nums),mn=n;\n        \n        int ones= count(begin(nums),end(nums),1);\n        \n        //if no one then no swap possible\n        if(ones==0) return 0;\n           \n        //For maintaining the number of one\'s zeroes\n        int curr_zero=0;\n        \n        int left=0,right=0;\n        \n        while(right<=ones-2){ \n            if(nums[right]==0) curr_zero++;\n            right++;\n        }\n        \n        if(nums[right]==0) curr_zero++;\n        \n        \n        //Keep on shrinking window size\n        while(left<n){\n            \n            //Number of Minimum Swaps in current window is equal to no. of places occulped by zeroes.\n            mn= min(mn,curr_zero);   \n            \n            //Shrink window from left\n            if(nums[left++]==0) curr_zero--;\n            \n            right=(right+1)%n; //circular array\n            \n            if(nums[right]==0) curr_zero++; //get count of last element in new window\n        }\n        \n        return mn;\n        \n    }
['C']