Easy to understand C++ solution || Sliding window || 🔥Beats 91%, O(n)
Intuition\nProblem can be approached using a sliding window technique to find the segment with the maximum number of 1\'s, as this will result in the minimum sw
# Intuition\nProblem can be approached using a sliding window technique to find the segment with the maximum number of 1\'s, as this will result in the minimum swaps required to group all the 1\'s together.\n\n\n# Approach\n1. **Calculate Total Ones**:\n   - Count the total number of 1\'s in the array, which determines the window size for the sliding window approach.\n\n2. **Initialize Window**:\n   - Calculate the number of 1\'s in the initial window of size equal to `total_ones`.\n\n3. **Sliding Window Technique**:\n   - Use two pointers `i` and `j` to represent the start and end of the current window.\n   - As the window slides over the array, adjust the count of `current_ones` by decrementing when leaving a 1 and incrementing when entering a 1.\n   - Compute the minimum swaps needed by comparing `total_ones - current_ones` for each window position.\n\n4. **Circular Array Handling**:\n   - To account for the circular nature of the array, use the `modulo` operation to wrap the window around the array when the end is reached.\n\n# Complexity\n- Time complexity:\n$$O(N)$$, where $$N$$ is the length of the input array. The array is traversed twice: once to count the 1\'s and once for the sliding window.\n- Space complexity:\n$$O(1)$$, as no additional space proportional to the input size is used.\n# Code\n```\nclass Solution {\npublic:\n    int minSwaps(vector<int>& nums) {\n        int n = nums.size();\n        int total_ones = 0;\n\n        // Calculate the total number of ones in the array\n        for (int i = 0; i < n; i++) {\n            if (nums[i] == 1) total_ones++;\n        }\n\n        // Count ones in the initial window\n        int current_ones = 0;\n        for (int i = 0; i < total_ones; i++) {\n            if (nums[i] == 1) current_ones++;\n        }\n\n        // Initialize variables for sliding window\n        int i = 0, j = total_ones - 1, minSwaps = INT_MAX;\n\n        // Slide the window over the circular array\n        while (i < n) {\n            // Update the minimum number of swaps needed\n            minSwaps = min(minSwaps, total_ones - current_ones);\n\n            // Slide the window to the right\n            if (nums[i++] == 1) current_ones--;\n            j++;\n\n            // Handle wrap-around using modulo operation\n            int index = j;\n            if (j >= n) index = j - n;\n            if (nums[index] == 1) current_ones++;\n        }\n\n        return minSwaps;\n    }\n};\n```\n## \uD83D\uDE80 Upvote Alert! \uD83D\uDE80\nHey, awesome coder! If you found this explanation and solution helpful, please hit that upvote button! It\'ll make my day brighter than a perfectly optimized algorithm and keep me motivated to churn out more helpful content. \uD83C\uDF1F\n\nWhy upvote, you ask?\n\nYou\'ll make me smile \uD83D\uDE0A\nIt gives me the energy to write solutions for more problems \uD83D\uDCAA\nYou\'ll be spreading good coding karma in the community \u2728\nSo go ahead, smash that upvote button! You know you want to! \uD83D\uDD3C\n
['Array', 'Sliding Window', 'C++']