Interview Ready Solution | Easy To Understand | Sliding Window
I understand this might not be the best leetcode runtime or memory usuage solution but it is the most understandble for interview purpose.\n\n```\nclass Solutio
I understand this might not be the best leetcode runtime or memory usuage solution but it is the most understandble for interview purpose.\n\n```\nclass Solution {\n    //Time: O(n), where n represent the length of nums\n    //Space: O(1), no need to store any data in any DS\n    public int minSwaps(int[] nums) {\n        //count the total numbers of ones\n        int totalOnes = 0;\n        for(int one : nums){\n            if(one == 1) totalOnes++;\n        }\n        \n        //initlize the length of given array, start/end of sliding window, current window ones, and swaps which holds length of given array\n        int n = nums.length, \n            start = 0,\n            end =0, \n            currentWindowOnes = 0, \n            swaps = n;\n        \n        //count the ones of first window\n        while(end < totalOnes){\n            if(nums[end] == 1) currentWindowOnes++;\n            \n            end++;\n        }\n        //calculate the first window swaps required\n        swaps = totalOnes - currentWindowOnes;\n        \n        //2 * n because of circular array\n        while(end < 2 * n){\n            //if the current start of circular array is 1 then decrease the current window ones\n            if(nums[start % n] == 1) currentWindowOnes--;\n            \n            //if the current end of circular array is 1 then increase the current window ones\n            if(nums[end % n] == 1) currentWindowOnes++;\n            \n            //increase both pointers\n            start++;\n            end++;\n            \n            //find the minmum swaps of current window\n            swaps = Math.min(swaps, totalOnes - currentWindowOnes);\n        }\n        \n        return swaps;\n    }\n}
['Sliding Window', 'Java']