<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p>

<p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>.</p>

<p>Given a <strong>binary</strong> <strong>circular</strong> array <code>nums</code>, return <em>the minimum number of swaps required to group all </em><code>1</code><em>&#39;s present in the array together at <strong>any location</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,0,1,1,0,0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Here are a few of the ways to group all the 1&#39;s together:
[0,<u>0</u>,<u>1</u>,1,1,0,0] using 1 swap.
[0,1,<u>1</u>,1,<u>0</u>,0,0] using 1 swap.
[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).
There is no way to group all 1&#39;s together with 0 swaps.
Thus, the minimum number of swaps required is 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,1,1,0,0,1,1,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are a few of the ways to group all the 1&#39;s together:
[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).
[1,1,1,1,1,0,0,0,0] using 2 swaps.
There is no way to group all 1&#39;s together with 0 or 1 swaps.
Thus, the minimum number of swaps required is 2.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,0,0,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the 1&#39;s are already grouped together due to the circular property of the array.
Thus, the minimum number of swaps required is 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

/*
Exhaustive Search : for each index i , count the total swaps required to group all ones at position i to j (j=i+countOfOnes-1) 
*/
//O(N*N) Time O(1)Space
class Solution1 {
    public:
    int minSwaps(vector<int>& nums) {
        int countOnes= getCountOfOnes(nums);

        int ans=countOnes; //at max countOnes swaps may be needed
        for(int i=0;i<nums.size();i++){
            //getting the number of swaps required to group ones at index [i,i+countOnes-1]
            ans=min(ans,getSwapsRequired(i,i+countOnes-1,nums));
        }
        return ans;
    }

    int getSwapsRequired(int l,int r,const vector<int> &nums){
        //number of swaps required between l and r is as good as number of zeros between l and r 
        int swaps=0,n=nums.size();
        for(int i=l;i<=r;i++){
            if((i<n && !nums[i]) or (i>=n && !nums[i%n]))swaps++;
        }
        return swaps;
    }
    
    int getCountOfOnes(const vector<int> &nums){
        int countOnes=0;
        for(int i:nums)
            if(i)countOnes++;
        return countOnes;
    }

};



/*
Optimized Solution1 : instead of iterating for i to j every time, preprocessing the number of zeros (or number of swaps) between any indices i and j 
*/
//O(N) Time O(N)Space
class Solution {

public:
    int minSwaps(vector<int>& nums) {
        int countOnes= getCountOfOnes(nums);
        if(countOnes==0)return 0;
        vector<int> prefixZeros(nums.size(),0); //for every i : stores the num of zeros from 0 to i
        preProcessPrefixZeros(nums,prefixZeros);

        int ans=countOnes; //at max countOnes number of swaps may be needed
        for(int i=0;i<nums.size();i++){
            //getting the number of swaps required to group ones at index [i,i+countOnes-1]
            ans=min(ans,getSwapsRequired(i,i+countOnes-1,nums,prefixZeros));
        }
        return ans;
    }
private:
    void preProcessPrefixZeros(const vector<int> &nums,vector<int> &prefixZeros){

        //initializing for index 0
        prefixZeros[0] = nums[0]==0;
        
        for(int i=1;i<nums.size();i++){
            prefixZeros[i]=prefixZeros[i-1]+(nums[i]==0);
        }

    }
    int getSwapsRequired(int l,int r,const vector<int> &nums , const vector<int> &prefixZeros){
        //number of swaps required between l and r is as good as number of zeros between l and r 
        int n=nums.size();
        if(r<n)return prefixZeros[r] - (l==0 ?  0 : prefixZeros[l-1]);
        
        else{
            //the subarray from i to j consists of 2 parts : [l to n-1] , [0 to (r%n)]
            int end = prefixZeros[n-1]-prefixZeros[l-1];
            int start = prefixZeros[(r%n)];
            return start+end; 
        }

        assert(false);
    }
    
    int getCountOfOnes(const vector<int> &nums){
        int countOnes=0;
        for(int i:nums)
            if(i)countOnes++;
        return countOnes;
    }

};
46840