Full Explanation(image) easy 8 line with Bonus code.....👻
Bonus Code at the end of explanation\n\n\nProblem Understanding\nThe goal is to find the minimum number of swaps required to group all 1s together in a circular
**Bonus Code at the end of explanation**\n\n\n**Problem Understanding**\nThe goal is to find the minimum number of swaps required to group all 1s together in a circular array.\n\n**Solution :**\n**1.Get targeted ones window size**\n    by `window=nums.count(1)` or `window=sum(nums)`\n\n**2.ans and count_1 to store answer and initial results** \n    ans and count_1 are initialized to the sum of the first window elements of nums.\nn is the length of the array nums.\n\n**3. Sliding Window Approach**\nThe goal is to find the maximum number of 1s in any window of size window in the circular array. This is done using a sliding window technique with modulo.\n\n**4.In loop**\n    this loop updates ans as maximum ones in window of size window.\n**5.return Statement**\n    As minimum swaps are difference between total ones we want to get together and the maximum ones initaily availabale in size of window in nums array.\n**Summary**\nThe algorithm uses a sliding window approach to find the maximum number of 1s in any subarray of size window within a circular array. The minimum number of swaps is then the difference between the total number of 1s and this maximum value. This ensures that all 1s can be grouped together with the least number of swaps.\n\n**Example**\nConsider the array `nums = [1, 0, 1, 0, 1, 1, 0, 0]`\n\n1. Calculate window :\nwindow = sum(nums) = 1 + 0 + 1 + 0 + 1 + 1 + 0 + 0 = 4\n\n2. Initialize the first window:\nFor the first window of size k (first 4 elements):\n\n`Window 1: [1, 0, 1, 0]`\nCount of 1s: 2\n\n**Slide the window and update the count of 1s:**\n\nMove the window one position to the right, considering the circular nature of the array.\n\n`Window 2: [0, 1, 0, 1]`\nCount of 1s: 2\n\n`Window 3: [1, 0, 1, 1]`\nCount of 1s: 3\n\n`Window 4: [0, 1, 1, 0]`\nCount of 1s: 2\n\n**Fifth Window (Wrap around the array):**\n\n\n`Window 5: [1, 1, 0, 0]`\nCount of 1s: 2\n\n**Sixth Window (Wrap around the array):**\n\n`Window 6: [1, 0, 0, 1]`\nCount of 1s: 2\n\n**Maximum count of 1s in any window:** \nThe maximum count of 1s in any window is 3.\n\nCalculate the minimum swaps needed:\nMinimum swaps = Total 1s - Maximum count of 1s in any window = 4 - 3 = 1\n\nVisual Representation\nLet\'s visualize these windows and their counts of 1s:\n\n![image.png](https://assets.leetcode.com/users/images/e0877588-d314-4b87-bf58-736fa27fe674_1722603933.6551437.png)\n\nFrom this, we see that the maximum count of 1s in any window of size 4 is 3. Thus, the minimum number of swaps needed to group all 1s together is 1.\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```python3 []\nclass Solution:\n    def minSwaps(self, nums: List[int]) -> int:\n        # we want to group all ones together as the total 1 is the target window we want to make \n        window = sum(nums)\n        # initialize the total ones in first window\n        ans = count_1 = sum(nums[:window])\n        n=len(nums)\n        # loop to first to last window (using modulo to encounter circular array...)\n        for i in range(window,n+window):\n            # next element if 1 +1 else +0\n            count_1 += nums[i%n]\n            # windows left most element if 1 +1 else 0\n            count_1 -= nums[(i-window+n)%n]\n            # updating ans as maximum one_s in that window\n            ans = max(ans,count_1)\n        # returning window(targeted ones together) - maximum ones we get = so it will return minimum 0 (as 0 are swaps) \n        return window - ans\n```\n```C++ []\nclass Solution {\npublic:\n    int minSwaps(std::vector<int>& nums) {\n        int k = std::accumulate(nums.begin(), nums.end(), 0);\n        int n = nums.size();\n        int ans = 0, count_1 = 0;\n        \n        for (int i = 0; i < k; i++) {\n            count_1 += nums[i];\n        }\n        \n        ans = count_1;\n        \n        for (int i = k; i < n + k; i++) {\n            count_1 += nums[i % n];\n            count_1 -= nums[(i - k + n) % n];\n            ans = std::max(ans, count_1);\n        }\n\n        return k - ans;\n    }\n};\n```\n```java []\nclass Solution {\n    public int minSwaps(List<Integer> nums) {\n        int k = 0;\n        for (int num : nums) {\n            k += num;\n        }\n\n        int n = nums.size();\n        int ans = 0, count_1 = 0;\n        \n        for (int i = 0; i < k; i++) {\n            count_1 += nums.get(i);\n        }\n        \n        ans = count_1;\n        \n        for (int i = k; i < n + k; i++) {\n            count_1 += nums.get(i % n);\n            count_1 -= nums.get((i - k + n) % n);\n            ans = Math.max(ans, count_1);\n        }\n\n        return k - ans;\n    }\n}\n```\n**Bonus Code:**\n    if you can\'t understand concept of modulo (%) operation append window sized nums to the last of nums array to check circularly \n(time and space complexity is slithly increased)\n```Python3 []\nclass Solution:\n    def minSwaps(self, nums: List[int]) -> int:\n        k = sum(nums)\n        ans = count_1 = sum(nums[:k])\n        nums = nums + nums[:k]\n        n=len(nums)\n        for i in range(k,n):\n            count_1 += nums[i]\n            count_1 -= nums[i-k]\n            ans = max(ans,count_1)\n        return k - ans\n```\n\n\n***Upvote if you like it**\n![image.png](https://assets.leetcode.com/users/images/9ad5e2d6-6341-439a-ad8c-b40822e5a00d_1722604037.4411416.png)\n\n
['Sliding Window', 'C++', 'Java', 'Python3']