[c++] Sliding window O(N) Intuitive Approach
Sliding Window Approach: \n\nWe will use sliding window Approach to track the how many max no of 1 are together, So for this we find the no of 1 in nums array a
Sliding Window Approach: \n\nWe will use sliding window Approach to track the how many max no of 1 are together, So for this we find the no of 1 in nums array and we take window of that size we start sliding the window and we count how many no of 1 can be max present at a time in window If we got this then we just need to swap other 1 to neer-by position of large group of 1. \n\nTo track the window we use a queue and If new element which need to be pushed is 1 then you need to increment the currOne and we are tracking it\'s size if queue size is more than window size If yes then we pop one element and also if popped element is 1 then we need to decrement no of currOne. and we alway calculate the max of previous maxOne and currOne. After traversing though the array you need to return n - maxOne because that no of swap you need to to make all 1 together. We do traversing of first from (nums.size()-n) till end and then from 0 to end because it is a circular array. Please go through below code you will get the idea.\n\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int n=0;\n        for(auto it:nums){  // calculating the 1\n            if(it)\n                n++;\n        }\n        if(n==nums.size()){ // when all are one no need to do swapping\n            return 0;\n        }\n        queue<int> slid;\n        int maxONe=0; // no of 1 max 1 found in queue till that point of time\n        int currOne=0;// no of 1 present in queue\n        for(int i=nums.size()-n;i<nums.size();i++){//We start from nums.size()-n till end because this is round array\n            if(slid.size()>=n){// we will pop only when queue have n element\n                if(nums[i]){\n                    currOne++;// we increment 1 when we got new one\n                }                \n                slid.push(nums[i]);\n                if(slid.front()){\n                    currOne--; // we decrement when we got to know that top of queue which will initial element of \n\t\t\t\t\t\t\t\t\t//slide is 1 means currOne will need to be reduce\n                }\n                slid.pop();\n            }\n            else{\n                if(nums[i]){\n                    currOne++;\n                }\n                slid.push(nums[i]);\n            }\n            maxONe=max(maxONe,currOne);                \n        }\n        \n        for(int i=0;i<nums.size();i++){ // after traversing from last we are good to start from 0th indxe we don\'t need \n\t\t\t\t\t\t\t\t//to check queue is empty because from above loop we should n element in queue\n               if(nums[i]){\n                    currOne++;\n                }                \n                slid.push(nums[i]);\n                if(slid.front()){\n                    currOne--;\n                }\n                slid.pop();\n                maxONe=max(maxONe,currOne); \n        }\n        return n-maxONe;\n    }\n};\n```\n\nTOC=O(N)
['C']