✅💯🔥Simple Code🚀📌|| 🔥✔️Easy to understand🎯 || 🎓🧠Beats 100%🔥|| Beginner friendly💀💯
Beats 100%\n\n\n# Code for Java\n\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int s = nums.length;\n        int ones = 0;\n        for (in
# Beats 100%\n![Screenshot 2024-08-02 130351.png](https://assets.leetcode.com/users/images/84302174-e569-49c3-b42a-d02f421d7ee6_1722584070.9444041.png)\n\n# Code for Java\n```\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int s = nums.length;\n        int ones = 0;\n        for (int n: nums) {\n            ones+=n;\n        }\n        if (ones==0 || ones==s){\n            return 0;\n        }\n        int curr = 0;\n        for(int i=0; i<ones; i++) {\n            curr+=nums[i];\n        }\n        int maxi = curr;\n        for (int i=0; i<s; i++) {\n            curr-=nums[i];\n            curr+=nums[(i+ones)%s];\n            maxi=Math.max(maxi, curr);\n        }\n        return ones-maxi;\n    }\n}\n```\n# Code for C++\n```\n#include <iostream>\n#include <vector>\nusing namespace std;\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int s = nums.size();\n        int ones = 0;\n        for (int n : nums) {\n            ones += n;\n        }\n        if (ones == 0 || ones == s) {\n            return 0;\n        }\n        int curr = 0;\n        for (int i = 0; i < ones; i++) {\n            curr += nums[i];\n        }\n        int maxi = curr;\n        for (int i = 0; i < s; i++) {\n            curr -= nums[i];\n            curr += nums[(i + ones) % s];\n            maxi = max(maxi, curr);\n        }\n        return ones - maxi;\n    }\n};\n```\n# Code for Python\n```\ndef min_swaps(nums):\n    n = len(nums)\n    ones = sum(nums)\n    if ones==0 or ones==n:\n        return 0\n    curr = sum(nums[:ones])\n    maxi = curr\n    for i in range(n):\n        curr-=nums[i]\n        curr+=nums[(i+ones)%n]\n        maxi=max(maxi, curr)\n    return ones-maxi\n\n```\n# Code for JavaScript\n```\nfunction minSwaps(nums) {\n    const s = nums.length;\n    let ones = 0;\n    for (let n of nums) {\n        ones += n;\n    }\n    if (ones === 0 || ones === s) {\n        return 0;\n    }\n    let curr = 0;\n    for (let i = 0; i < ones; i++) {\n        curr += nums[i];\n    }\n    let maxi = curr;\n    for (let i = 0; i < s; i++) {\n        curr -= nums[i];\n        curr += nums[(i + ones) % s];\n        maxi = Math.max(maxi, curr);\n    }\n    return ones - maxi;\n}\n```\n# Code for C\n```\n#include <stdio.h>\nint minSwaps(int nums[], int s) {\n    int ones = 0;\n    for (int i = 0; i < s; i++) {\n        ones += nums[i];\n    }\n    if (ones == 0 || ones == s) {\n        return 0;\n    }\n    int curr = 0;\n    for (int i = 0; i < ones; i++) {\n        curr += nums[i];\n    }\n    int maxi = curr;\n    for (int i = 0; i < s; i++) {\n        curr -= nums[i];\n        curr += nums[(i + ones) % s];\n        maxi = (maxi > curr) ? maxi : curr;\n    }\n    return ones - maxi;\n}\n```\n![7be995b4-0cf4-4fa3-b48b-8086738ea4ba_1699897744.9062278.jpeg](https://assets.leetcode.com/users/images/fe5117c9-43b1-4ec8-8979-20c4c7f78d98_1721303757.4674635.jpeg)
['Array', 'C', 'Sliding Window', 'Python', 'C++', 'Java', 'JavaScript']