📌📌Sliding Window || Easy To Understand || C++ Code
Using Sliding Window\n\n Time Complexity :- O(N)\n\n Space Complexity :- O(N)\n\n\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n\n        i
* ***Using Sliding Window***\n\n* ***Time Complexity :- O(N)***\n\n* ***Space Complexity :- O(N)***\n\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n\n        int n = nums.size();\n\n        // count the no. of ones in nums\n\n        int count_1 = 0;\n\n        for(int i = 0; i < n; i++)\n        {\n            if(nums[i])\n            {\n                count_1++;\n            }\n        }\n\n        // if count of 1 is zero then return 0\n\n        if(count_1 == 0)\n        {\n            return 0;\n        }\n\n        // make a double_arr by concatenating nums to itself, for handling the case of circular array\n\n        vector<int> double_arr = nums;\n\n        for(int i = 0; i < n; i++)\n        {\n            double_arr.push_back(nums[i]);\n        }\n\n        // now find the maximum no. of ones in count_1 sized subarray\n\n        int maxi = 0;\n\n        int count = 0;\n\n        for(int i = 0; i < double_arr.size(); i++)\n        {\n            if(i < count_1)\n            {\n                if(double_arr[i])\n                {\n                    count++;\n                }\n            }\n            else\n            {\n                maxi = max(maxi, count);\n\n                if(double_arr[i - count_1])\n                {\n                    count--;\n                }\n\n                if(double_arr[i])\n                {\n                    count++;\n                }\n            }\n        }\n\n        maxi = max(maxi, count);\n\n        // count_1 - maxi will be required no. of swaps\n\n        return count_1 - maxi;\n    }\n};\n```
['C', 'Sliding Window', 'C++']