🔥 Java O(N) Explanation with Diagrams ✍🏻
\uD83D\uDCCC Sliding Window \n\n\n\n\n\n\n\n\nTime Complexity : O(N)   ||  Space Complexity : O(N)\n\n\n public int minSwaps(int[] nums) {\n        \n        in
# \uD83D\uDCCC Sliding Window \n<img width="50%" src="https://assets.leetcode.com/users/images/74c6717f-82e1-49ef-ac59-0def9f630ae6_1646821665.903593.png">\n<img width="50%" src="https://assets.leetcode.com/users/images/55496339-0d86-4821-b376-f7c1eb100d99_1646821665.8353672.png">\n<img width="50%" src="https://assets.leetcode.com/users/images/f3e7c571-5148-4f2f-849d-0031f7d37041_1646821665.8760197.png">\n<img width="50%" src="https://assets.leetcode.com/users/images/d98142ea-c40c-4865-9be9-9c7908e0d702_1646821664.4522345.png">\n\n[](http://)\n[](http://)\n\n**Time Complexity : O(N)   ||  Space Complexity : O(N)**\n\n```\n public int minSwaps(int[] nums) {\n        \n        int l = nums.length;\n\t\t//prefix array : storing ones count upto ith index\n        int[] ones = new int[l];\n        ones[0] = nums[0]==1 ? 1 : 0;\n        for(int i=1;i<l;i++){\n\t\t   if(nums[i] == 1) ones[i] = ones[i-1] + 1;\n\t\t   else ones[i] = ones[i-1];\n        }\n        \n        if(ones[l-1]==l || ones[l-1]==0) return 0;  // either all ones or no ones\n        \n        int ws = ones[l-1]; //window size = total no of ones in the array\n        int minSwaps = Integer.MAX_VALUE;\n        int si = 0, ei;\n\t\t\n        while(si<nums.length)\n        {\n            ei = (si+ws-1)%l;\n\t\t\tint totalones;\n\t\t\t\n            if(ei>=si)  totalones = ones[ei]-( si==0 ? 0 : ones[si-1]);     //normal window\n            else  totalones = ones[ei] + (ones[l-1]-ones[si-1]);     //circular window\n\t\t\t\n\t\t\tint swapsreq = ws-totalones;    // swaps req. = no. of zeroes in the window\n            if(swapsreq < minSwaps) minSwaps = swapsreq;\n            \n            si++;\n        }\n        \n        return minSwaps;\n    }\n```\n\n***\uD83D\uDCCC UPVOTE If you liked the explanation \uD83D\uDE04\uD83D\uDE0A***
['Sliding Window']