Same Concept of Sliding Window || O(N) in 3 Langs. || Margin(94%)
\n# Intuition Behind the Code\n### Fools yell from bot accounts, we just need to move on.. This is my soln with submission history attached below..After Several
\n# Intuition Behind the Code\n### Fools yell from bot accounts, we just need to move on.. This is my soln with submission history attached below..After Several attempts was able to provide soln with acceptable margin\n\n1. Counting 1s (`k`):\n   - First, the code calculates the total number of 1s in the array, denoted by `k`. This is because the task is to find the smallest window (or segment) of size `k` that contains the maximum number of 1s.\n\n2. Initial Window Calculation:\n   - The code then computes the number of 1s in the first window of size `k`. This is achieved by summing up the first `k` elements of the array. This sum is stored in the variable `cnt` and also in `mx` (as the initial maximum count of 1s in any window).\n\n3. Sliding Window Technique:\n   - The core part of the solution involves a sliding window approach to move through the array and calculate the number of 1s in each window of size `k`.\n   - For each step from `i = k` to `i = n + k - 1` (where `n` is the length of the array):\n     - The code updates the `cnt` by adding the next element in the window (`nums[i % n]`) and removing the element that is sliding out of the window (`nums[(i - k + n) % n]`).\n     - The maximum number of 1s found in any window of size `k` is updated in `mx`.\n\n4. Result Calculation:\n   - Finally, the result is derived as `k - mx`, which represents the minimum number of swaps needed. This is because `k` is the total number of 1s, and `mx` is the maximum number of 1s found in any window of size `k`. Therefore, the difference `k - mx` gives the minimum number of 0s that need to be swapped with 1s to achieve the desired grouping of 1s.\n\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n\n```python []\nclass Solution:\n    def minSwaps(self, nums: List[int]) -> int:\n        k = nums.count(1)\n        mx = cnt = sum(nums[:k])\n        n = len(nums)\n        for i in range(k, n + k):\n            cnt += nums[i % n]\n            cnt -= nums[(i - k + n) % n]\n            mx = max(mx, cnt)\n        return k - mx\n```\n```java []\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int k = Arrays.stream(nums).sum();\n        int n = nums.length;\n        int cnt = 0;\n        for (int i = 0; i < k; ++i) {\n            cnt += nums[i];\n        }\n        int mx = cnt;\n        for (int i = k; i < n + k; ++i) {\n            cnt += nums[i % n] - nums[(i - k + n) % n];\n            mx = Math.max(mx, cnt);\n        }\n        return k - mx;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int k = accumulate(nums.begin(), nums.end(), 0);\n        int n = nums.size();\n        int cnt = accumulate(nums.begin(), nums.begin() + k, 0);\n        int mx = cnt;\n        for (int i = k; i < n + k; ++i) {\n            cnt += nums[i % n] - nums[(i - k + n) % n];\n            mx = max(mx, cnt);\n        }\n        return k - mx;\n    }\n};\n```\n\n# Code built after many attempts:\n![Screenshot_2-8-2024_65117_leetcode.com.jpeg](https://assets.leetcode.com/users/images/dec9dc7f-d811-4de4-a632-e5448759e0a3_1722561726.9943426.jpeg)\n![Screenshot_2-8-2024_65131_leetcode.com.jpeg](https://assets.leetcode.com/users/images/a9648e94-7b03-4842-9f93-a30eca7c2dc7_1722561735.4200552.jpeg)\n\n
['Array', 'Sliding Window', 'Python', 'C++', 'Java', 'Python3']