Simple Fixed Length Sliding Window Approach
Approach
Count the total number of 1's in the array:

Iterate through the array and count how many 1s are present. Let's call this count k.


Sliding window app
<!-- # Intuition -->
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
* Count the total number of 1's in the array:
  * Iterate through the array and count how many 1s are present. Let's call this count k.
* Sliding window approach:
  * Use a sliding window of size k to find the maximum number of 1s within any subarray of length k.
* Window shifting:
  * Start with the window at the beginning of the array.
  * Extend the window to the right, incrementing e. If the number is 1, increment a counter one.
  * When the window exceeds size k, slide the window to the right by incrementing s (start of the window). If the number at s is 1, decrement one.
* Track the maximum number of 1's in the window:
  * During each window slide, update the maxi to track the maximum number of 1s encountered in a window of size k.
* Calculate minimum swaps:
  * The minimum swaps required to group all 1s together is the difference between k (total number of 1s) and maxi (maximum number of 1s in a sliding window of size k).
* Return the result:
  * The result is k - maxi, which represents the minimum number of swaps needed.

# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int minSwaps(vector<int>& nums) {
        int n = nums.size();

        int k = 0;
        for(int i = 0; i < n; i++){
            if(nums[i] == 1) k++;
        }

        int s = 0, e = 0;
        int one = 0, maxi = 0;
        while(e < (n + k)){
            if(e < k){
                if(nums[e] == 1) one++;
            }
            else{
                if(nums[e % n] == 1) one++;
                if(nums[s] == 1) one--;
                s++;
            }
            
            maxi = max(maxi, one);
            e++;
        }

        return k - maxi;
    }
};
```
['Array', 'Sliding Window', 'C++']