"Optimal Code | TC O(n) | SC O(1) | Beats 93% | Easy to understand"
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to find the minimum number of swaps required to group all the 1s together i
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to find the minimum number of swaps required to group all the 1s together in a given binary array. The approach revolves around the idea of using a sliding window to find the position where the group of 1s can be moved to minimize the number of swaps.\n\n**Intuition Behind the Code**\n\n**Counting the 1s (once):** This tells us the target size of the window that should contain all the 1s after the optimal arrangement.\n**Sliding Window:** The sliding window technique is used to efficiently calculate the number of 1s in each possible window of size once by maintaining a running count (ow) and updating it as the window slides. This avoids recalculating the sum from scratch for each new position of the window.\n**Maximizing 1s in the Window (max1):** By keeping track of the maximum number of 1s in any window, we determine the best case scenario with the least number of swaps needed.\n**Final Calculation (once - max1):** This gives the number of 0s that need to be swapped out to achieve the optimal window with all 1s grouped together.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Count the Total Number of 1s (once):\n\n    The total number of 1s in the array (once) determines the size of the window that needs to be created, where ideally all 1s should be grouped together. This is because if there are k 1s in the array, the resulting group of 1s after swaps should also have a window of size k.\n\n2. Calculate the Initial Number of 1s in the First Window (ow):\n\n    Start by calculating the number of 1s in the first window of size once starting from the beginning of the array. This helps establish a baseline for the number of 1s we can expect to find in any window of this size.\n\n3. Sliding Window to Find Maximum Number of 1s in Any Window of Size once:\n\n    Slide the window across the array to check each possible group of once elements. For each shift, update the count of 1s in the current window by:\n    Adding 1 if the element entering the window (from the right) is 1.\n    Subtracting 1 if the element exiting the window (from the left) is 1.\n    Keep track of the maximum number of 1s (max1) found in any window of this size.\n\n4. Calculate the Minimum Number of Swaps:\n\n    The minimum number of swaps required to group all 1s together is given by the difference between the total number of 1s (once) and the maximum number of 1s found in any window (max1). This is because the difference represents the number of 0s in the optimal window, which need to be swapped out with 1s from outside the window.\n\nFor maintaining the circular array case, we can use "%" of len(arr).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n\n\n# Code\n```\nclass Solution:\n    def minSwaps(self, nums: List[int]) -> int:\n        once=nums.count(1)\n        ow=sum(nums[:once])\n        n=len(nums)\n        if ow==n:\n            return 0\n        max1=0\n        for i in range(len(nums)):\n            if nums[i%n]==0 and nums[(i+once)%n] ==1:\n                ow+=1\n            elif nums[i%n]==1 and nums[(i+once)%n] ==0:\n                ow-=1\n            max1=max(max1,ow)\n        return once-max1\n        \n```
['Sliding Window', 'Python3']