BEATS 98% || 4STEPS XPLANATION || JAVA|| EASY BEGINNER
BOISS N PRETTIES UPVOTE!! KRDE NA BRO ;() TUJHE MOMOS KI KASAM.\n\n1. Prefix Sum Calculation:\n   - Well No explanation for this.\n\n2. Initial Conditions:\n   
# BOISS N PRETTIES UPVOTE!! KRDE NA BRO ;() TUJHE MOMOS KI KASAM.\n\n1. **Prefix Sum Calculation:**\n   - Well No explanation for this.\n\n2. **Initial Conditions:**\n   - `i` and `j` are initialized to 0 and `totalOnes`.\n   - `totalOnes` represents the total number of 1s in the array.\n\n3. **Sliding Window and Swaps:**\n   - The `while` loop will run till `i` reaches the end of the array.\n   - Inside the loop (PAY ATTENTION):\n     - If `j` is within the valid range (less than or equal to `totalOnes`), the minimum swaps are calculated using the prefix sum and the current window size (`j - i`).\n     - If `j` is outside the valid range, the minimum swaps are calculated considering the wrap-around of the array.\n     - `i` and `j` are incremented to move the sliding window.\n     - If `j` reaches the end of the array, it is reset to 1 to handle the circular nature.\n     - Look if you are not able to get this create a dry run and you will notice it happening say for nums=1,0,1,0,0,1.\n\n4. **Return Minimum Swaps:**\n   - The final `ans` variable holds the minimum number of swaps required. The code returns this value YEAH BBY!.\n\n\n\n# Code\n```java []\nclass Solution {\n    public int minSwaps(int[] nums) {\n        int [] prefixSum=new int [nums.length+1];\n        for(int i=1;i<=nums.length;i++){\n            prefixSum[i]=prefixSum[i-1]+nums[i-1];\n        }\n        int i=0;\n        int totalOnes=prefixSum[nums.length];\n        int j=totalOnes;\n        int ans=Integer.MAX_VALUE;\n        while(i<prefixSum.length){\n            if(j>=totalOnes){\n                ans=Math.min(ans,totalOnes-(prefixSum[j]-prefixSum[i]));\n            }\n            else{\n                ans=Math.min(ans,totalOnes-(prefixSum[prefixSum.length-1]+prefixSum[j]-prefixSum[i]));\n            }\n            i++;\n            j++;\n            if(j==prefixSum.length){\n                j=1;\n            }\n        }\n        return ans;\n    }\n}\n```
['Sliding Window', 'Java']