C++ | Sliding window | Intuition explained
```\n/\n1. For simplicity let us assume that the array is not circular.\n2. The outcome which we expect is a continuos window of ones\n3. We count the total num
```\n/*\n1. For simplicity let us assume that the array is not circular.\n2. The outcome which we expect is a continuos window of ones\n3. We count the total number of ones in that input, this will be our window size, let us call it K\n4. We now iterate over all the windows of K size, in a sliding manner, calculating the diff = K-number of ones in current window\n5. Here diff gives us the no of swaps for that window. We maintain the value of min diff encountered and return that as an error.\n6. Now to deal with the circular array case, let\'s append the input array to itself\nFor ex: [1,1,0,0,1]\n\nmax no ones = K = 3\nmodfied i/p = [1,1,0,0,1,1,1,0,0,1]\nprocesing all windows ok size 3\ndiff = K-count of ones \n(1,1,0) 3-2 = 1\n(1,0,0) 3-1 = 2\n(0,0,1) 3-1 = 1\n(0,1,1) 3-2 = 1\n(1,1,1) 3-3 = 0\n(1,1,0) 3-2 = 1\n(1,0,0) 3-1 = 2\n(0,0,1) 3-1 = 2\n\nThe minimum value of diff = min swaps required = 0 \n\nNote : Instead of actually using extra space and appending input array to iteself, we can use the mod operator to access the values\n\n*/\n\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        \n        int onesCnt=0;\n        \n        int n = nums.size();\n        for(int n : nums)\n            if(n)\n                onesCnt++;\n        \n        int start=0, end=0, ans=INT_MAX, currOnes=0;\n        for(int end=0; end<2*n; end++)\n        {\n            currOnes+=nums[end%n];\n            if(end-start >= onesCnt-1)\n            {\n                    ans = min(ans, onesCnt-currOnes);\n                    currOnes-=nums[(start++)%n];\n            } \n         \n        }\n        \n        return ans;\n        \n    }\n};\n
['Sliding Window', 'C++']