Easy Sliding window | with vid explanation
Vid Explanation\nhttps://youtu.be/xsH9DvzkRPY\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires finding the minim
# Vid Explanation\nhttps://youtu.be/xsH9DvzkRPY\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the minimum number of swaps to group all 1s together in a circular array. Intuitively, we can focus on a window of size equal to the total number of 1s. We need to find the minimum number of zeros within this window as that\'s directly related to the number of swaps required.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe\'ll use a sliding window approach:\n\n1. Calculate the total number of 1s (window size).\n2. Count the number of zeros within the initial window.\n3. Slide the window and update the count of zeros.\n4. Keep track of the minimum number of zeros encountered.\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n\n        int window = 0;\n\n        for(auto num: nums){\n\n            if (num == 1) window++;\n            \n        }\n\n        int n = nums.size();\n        int zero = 0;\n\n        for(int i=0;i<window;i++){\n\n            if(nums[i]==0) zero++;\n            \n        }\n\n        int swap = zero;\n\n        for(int i=window;i<n + window;i++){\n\n            if(nums[i % n] == 0) zero++;\n\n            if(nums[i-window] == 0) zero--;\n\n            swap = min(swap,zero);\n            \n        }\n\n        return swap;\n        \n    }\n};\n```
['C++']