Best Approach in Easy words (Javascript, C++, Java and Python)
Approach\nSince we have to group all 1s in the nums, we will first calculate the total number of 1s. Which will be the windowSize.\n\nAfter this we will count t
# Approach\nSince we have to group all 1s in the nums, we will first calculate the total number of 1s. Which will be the `windowSize`.\n\nAfter this we will count the 1s in that sliding window. And then iterate through the entire length of the nums array. By doing this we can get the maximum number of 1s in the window `max1s`\n\nOnce we have the max1s, we can simply substract the max number of 1s from total number of 1s to get the minimum swaps (converted from 0 to 1).\n\n# Complexity\n- Time complexity:\nO(n)\n\n\n- Space complexity:\nO(n)\n\n# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minSwaps = function(nums) {\n    const n = nums.length;\n    \n    const windowSize = nums.reduce((sum, num) => sum + num, 0);\n    \n    let slideWindow = nums.slice(0, windowSize);\n\n    let slide1s = slideWindow.reduce((sum, num) => sum + num, 0);\n    let max1s = slide1s;\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[(windowSize+i) % n] === 1) {\n            slide1s++; // Adding New one\n        }\n\n        if (nums[i] === 1) {\n            slide1s--; // Removing old one\n        }\n\n        max1s = Math.max(max1s, slide1s);\n    }\n\n    return windowSize - max1s; // Minimum swaps;\n};\n```\n```C++ []\n#include <vector>\n#include <numeric>\n#include <algorithm>\n\nint minSwaps(std::vector<int>& nums) {\n    int n = nums.size();\n    \n    int windowSize = std::accumulate(nums.begin(), nums.end(), 0);\n    \n    std::vector<int> slideWindow(nums.begin(), nums.begin() + windowSize);\n\n    int slide1s = std::accumulate(slideWindow.begin(), slideWindow.end(), 0);\n    int max1s = slide1s;\n    for (int i = 0; i < n; ++i) {\n        if (nums[(windowSize + i) % n] == 1) {\n            slide1s++; // Adding new one\n        }\n\n        if (nums[i] == 1) {\n            slide1s--; // Removing old one\n        }\n\n        max1s = std::max(max1s, slide1s);\n    }\n\n    return windowSize - max1s; // Minimum swaps\n}\n```\n```Java []\nimport java.util.*;\n\npublic class Solution {\n    public int minSwaps(int[] nums) {\n        int n = nums.length;\n        \n        int windowSize = Arrays.stream(nums).sum();\n        \n        int slide1s = 0;\n        for (int i = 0; i < windowSize; i++) {\n            slide1s += nums[i];\n        }\n        int max1s = slide1s;\n        for (int i = 0; i < n; i++) {\n            if (nums[(windowSize + i) % n] == 1) {\n                slide1s++; // Adding new one\n            }\n\n            if (nums[i] == 1) {\n                slide1s--; // Removing old one\n            }\n\n            max1s = Math.max(max1s, slide1s);\n        }\n\n        return windowSize - max1s; // Minimum swaps\n    }\n}\n```\n```python []\ndef minSwaps(nums):\n    n = len(nums)\n    \n    windowSize = sum(nums)\n    \n    slideWindow = nums[:windowSize]\n    \n    slide1s = sum(slideWindow)\n    max1s = slide1s\n    for i in range(n):\n        if nums[(windowSize + i) % n] == 1:\n            slide1s += 1  # Adding new one\n\n        if nums[i] == 1:\n            slide1s -= 1  # Removing old one\n\n        max1s = max(max1s, slide1s)\n\n    return windowSize - max1s  # Minimum swaps\n\n```\n\n\n---\n\n\n\n**If my solution was helpful do give it a UpVote so that others can get this easy solution as well.**\n\n\n
['Array', 'Sliding Window', 'Python', 'C++', 'Java', 'JavaScript']