java commented solution 100% faster o(n) time,o(1) space. easy to understanded commented code
pls do comment if any thing is not clear\n\n\nclass Solution {\n    public int minSwaps(int[] nums) {\n                                                         
pls do comment if any thing is not clear\n\n```\nclass Solution {\n    public int minSwaps(int[] nums) {\n                                                                          //sliding window approach\n        int n=nums.length;\n        /*\n                          sliding window pattern\n           step 1->we\'ll first calculate total no\'s of ones in our array \n                                **edge case:>(if(n==1||totalnoOfOne<=1)  return 0) \n           step 2->then we\'ll traverse in our array some what like sliding window\n           step 3->and the size of the window will be the total no of ones in our array\n           step 4->and from every window we will try to find no of ones present in that window\n           step 5-> with hlp of no of ones we\'ll calculate no of zeros minzero=(totalOnes-onesinwindow) \n                                     note:->thats what gonna be min no of swaps(minSwaps)\n           step 6-> at the end minzero will be the no of swaps required\n           \n        */\n        //calulate total no of one\'s\n        int totalNoOfOnes=0;\n        for(int i=0;i<n;i++){\n            if(nums[i]==1)totalNoOfOnes++;\n        }\n        \n        //edge case \n               //mentioned below step 1\n        if(n==1||totalNoOfOnes<=1)return 0;\n            \n        \n        \n        int preOne=0;\n        for(int i=totalNoOfOnes-2;i>=0;i--){\n            if(nums[i]==1)preOne++;\n        }\n        /*\n        about pre one----------------------------------\n                                                      |\n                                                      V\n        */\n        \n        /*\n            cause the array is circular that\'s why we are calculating preOne what it is, will be clear with the given example             \n            example -> [0,1,0,0,0,1,1,0,0]\n            so for this example we need to calculate total no one\'s present cause that what goona be the size of our window\n            so, total no of one\'s=3\n                index-> 0,1,2,3,4,5,6,7,8    \n                       [0,1,0,0,0,1,1,0,0]\n                                        ^  \n                                        |\n            ******so for this index 8 this is what are window of size 3 will look like*****\n               index-> 0,1,2,3,4,5,6,7,8    \n                       [0,1,0,0,0,1,1,0,0]\n                        ^ ^             ^  \n                        | |             |\n\n         *********in window this is what are preone will be***** \n                index-> 0,1,2,3,4,5,6,7,8    \n                       [0,1,0,0,0,1,1,0,0]\n                        ^ ^             ^  \n                        | |             |\n                       <--->\n                       preOne\n        */\n        \n        \n        // sliding window  which will give minzeros out of all windows cause thats wahat gonna be min swaps (minSwaps) required:)  \n           \n        int l=n-1,r=(n+totalNoOfOnes-2);\n        int minSwaps=Integer.MAX_VALUE;   //or minZeros\n        while(l>=0){\n            preOne+=nums[l];\n            minSwaps=Math.min(minSwaps,totalNoOfOnes-preOne); //for this go to step5\n            preOne-=nums[r%n];\n            l--;\n            r--;\n        }\n        return minSwaps;\n    }  \n} \n```\ndo consider upvote if its helpfull :)\n\t
['Sliding Window', 'Java']