<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>

class Solution {
public:
    int minSwaps(vector<int>& nums) {
        int i,c=0;
        vector<int> a;
        int j=nums.size();
        for (i=0;i<nums.size();i=i+1)
        {
            if (nums[i]==1)
            {
                c=c+1;
                a.push_back(i);
            }
        }
        if (c==0)
        {
            return 0;
        }
        if (c==nums.size())
        {
            return 0;
        }
        int m=0,u=0,l=0;
        for (i=0;i<c;i=i+1)
        {
            if (nums[i]==1)
            {
                m=m+1;
            }
        }
        u=max(u,m);
        for (i=c;i<(2*j);i=i+1)
        {
            if (nums[(i%j)]==1)
            {
                m=m+1;
            }
            if (nums[l]==1)
            {
                m=m-1;
            }
            u=max(u,m);
            l=(l+1)%j;
        }
        return (c-u);
       /* i=0;
        int t,m,u=0;
        while (i<a.size())
        {
            t=a[i];
            m=0;
            j=i;
            while (j<a.size() and (a[j]-t)<c)
            {
                m=m+1;
                j=j+1;
            }
            i=i+1;
            u=max(m,u);
        }
        return (c-u);*/
      /*  int l=0,r=c-1;
        int u=-1;
        pair<int,int> p;
        int m;
        int k,j,x=nums.size();
        for (l=0;l<(2*nums.size())-c;l=l+1,r=r+1)
        {
            m=0;
            for (j=l;j<=r;j=j+1)
            {
                k=j%x;
                if (nums[k]==1)
                {
                    m=m+1;
                }
            }
            if (m>=u)
            {
                p.first=l;
                p.second=r;
                u=m;
            }
        }
        i=0;
        m=0;
        for (j=p.first;j<=p.second;j=j+1)
            {
                k=j%x;
                if (nums[k]==0)
                {
                    m=m+1;
                }
            }
        return m;*/
    }
};
60640