[JAVA] O(N) Sliding Window Solution || Read the commented code if you have time.
\n    public int minSwaps(int[] arr) {\n        int count = 0;\n        int n=arr.length;\n        for (int i = 0; i < n; ++i)\n        count+=arr[i];\n\n      
\n    public int minSwaps(int[] arr) {\n        int count = 0;\n        int n=arr.length;\n        for (int i = 0; i < n; ++i)\n        count+=arr[i];\n\n        // Find unwanted elements in current\n        // window of size \'count\'\n        int bad = 0;\n        for (int i = 0; i < count; ++i)\n        if (arr[i] == 0)\n            ++bad;\n\n        // Initialize answer with \'bad\' value of\n        // current window\n        int ans = bad;\n        for (int i = 0, j = count; j < 2*n; ++i, ++j) {\n\n        // Decrement count of previous window\n        if (arr[i%n] == 0)\n            --bad;\n\n        // Increment count of current window\n        if (arr[j%n] == 0)\n            ++bad;\n\n        // Update ans if count of \'bad\'\n        // is less in current window\n        ans = Math.min(ans, bad);\n        }\n        return ans;\n    }\n
['Array', 'Sliding Window', 'Java']