Use Silding Window And Find the Window Needing Min. Swaps | Java | C++ | [Vidoeo Solution]
Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/LRjOvAslzCg\n# Code\nJava\n\nclass Solution {\n    public int minSwa
# Intuition, approach, and complexity discussed in video solution in detail\nhttps://youtu.be/LRjOvAslzCg\n# Code\nJava\n```\nclass Solution {\n    public int minSwaps(int[] nums) {\n        return Math.min(findMinSwaps(nums, 0), findMinSwaps(nums, 1));\n    }\n    private int findMinSwaps(int nums[], int val){\n        int valCnt = 0, len = nums.length;\n        for(var num : nums)if(num == val)valCnt++;\n        int left = 0, right = 0, windowSize = valCnt;\n        int oppValsInCurrWindow = 0, opsVal = (val == 1) ? 0 : 1;\n        while(right < windowSize){\n            if(nums[right++] == opsVal)oppValsInCurrWindow++;\n        }\n        int minOppValsInWindowsTillNow = oppValsInCurrWindow;\n        while(right < (len)){\n            if(nums[left++] ==  opsVal)oppValsInCurrWindow--;\n            if(nums[right++] == opsVal) oppValsInCurrWindow++;\n            minOppValsInWindowsTillNow = Math.min(minOppValsInWindowsTillNow, oppValsInCurrWindow);\n        }\n        return minOppValsInWindowsTillNow;\n    }\n}\n```\nC++\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        return min(findMinSwaps(nums, 0), findMinSwaps(nums, 1));\n    }\n\nprivate:\n    int findMinSwaps(vector<int>& nums, int val) {\n        int valCnt = 0, len = nums.size();\n        for(auto num : nums) if(num == val) valCnt++;\n        \n        int left = 0, right = 0, windowSize = valCnt;\n        int oppValsInCurrWindow = 0, opsVal = (val == 1) ? 0 : 1;\n        \n        while(right < windowSize) {\n            if(nums[right++] == opsVal) oppValsInCurrWindow++;\n        }\n        \n        int minOppValsInWindowsTillNow = oppValsInCurrWindow;\n        \n        while(right < len) {\n            if(nums[left++] == opsVal) oppValsInCurrWindow--;\n            if(nums[right++] == opsVal) oppValsInCurrWindow++;\n            minOppValsInWindowsTillNow = min(minOppValsInWindowsTillNow, oppValsInCurrWindow);\n        }\n        \n        return minOppValsInWindowsTillNow;\n    }\n};\n\n```
['Array', 'Sliding Window', 'C++', 'Java']