Java - Sliding window - O(n) - Easy understanding
To solve this problem lets understand the basic sliding window technique :\n\t\n\t1. We should go for sliding window when we want to find the minimum or maximum
To solve this problem lets understand the basic sliding window technique :\n\t\n\t1. We should go for sliding window when we want to find the minimum or maximum range in a given array\n\t2. Sliding window has 2 methods as fixed and dynamic range\n\nLets understand the problem more clearly:\n\t1. first we need to group 1\'s inorder to do that we need to first find the number of 1\'s available\n\t2. If there are no one\'s or all one\'s we can directly return zero as we don\'t have to process anything\n\t3. After knowning the 1\'s we just have to find the number of zero\'s from 0 to n for each window of size equal to one. (since we know the number of one this problem is fixed sliding window)\n\t4. now initial number of zeroes is found when i==0 then we can just see the element leaving the sliding window i-1 and element adding into the sliding window i+totalOne-1\n\t5. If both the elements are 0 or 1 then the count of 0\'s remain same, if element added into sliding window is 0 and leaving sliding window is 1 then count of 0 is increasing and vice versa\n\t6. And one last thing to note is -> since it is given is  circular array we have to modulo by len so that for later part where i+totalOne-1>len it automatically go within the range of len.\n\nHope its easy to understand, please upvote if its helpful.. Happy learning :)\n```\n\tpublic int minSwaps(int[] nums) {\n\t\tint len = nums.length;\n\t\tint totalOne = 0;\n\n\t\tfor (int i = 0; i < len; i++) {\n\t\t\tif (nums[i] == 1)\n\t\t\t\ttotalOne++;\n\t\t}\n\t\tif (len == totalOne || totalOne == 0)\n\t\t\treturn 0;\n\t\tint min = Integer.MAX_VALUE;\n\t\tint i = 0;\n\t\tint count = 0;\n\t\tfor (int k = i; k < totalOne; k++) {\n\t\t\tif (nums[k] == 0)\n\t\t\t\tcount++;\n\t\t}\n\t\tmin = Math.min(min, count);\n\t\ti++;\n\t\tfor (; i < len; i++) {\n\t\t\tint j = (i + totalOne - 1) % len;\n\t\t\tif (nums[j] == 0 && nums[i - 1] == 1)\n\t\t\t\tcount++;\n\t\t\telse if (nums[j] == 1 && nums[i - 1] == 0)\n\t\t\t\tcount--;\n\t\t\tmin = Math.min(min, count);\n\t\t}\n\t\treturn min;\n\t}\n\t\t
['Sliding Window', 'Java']