<p>You are given an integer array, <code>nums</code>, and an integer <code>k</code>. <code>nums</code> comprises of only <code>0</code>&#39;s and <code>1</code>&#39;s. In one move, you can choose two <strong>adjacent</strong> indices and swap their values.</p>

<p>Return <em>the <strong>minimum</strong> number of moves required so that </em><code>nums</code><em> has </em><code>k</code><em> <strong>consecutive</strong> </em><code>1</code><em>&#39;s</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,0,0,1,0,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> In 1 move, nums could be [1,0,0,0,<u>1</u>,<u>1</u>] and have 2 consecutive 1&#39;s.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,0,0,0,0,0,1,1], k = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> In 5 moves, the leftmost 1 can be shifted right until nums = [0,0,0,0,0,<u>1</u>,<u>1</u>,<u>1</u>].
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,0,1], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong> nums already has 2 consecutive 1&#39;s.
</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 <code>0</code> or <code>1</code>.</li>
	<li><code>1 &lt;= k &lt;= sum(nums)</code></li>
</ul>


class Solution {
public:
    int minMoves(vector<int>& nums, int k) {

        int n=nums.size();
        if(k==1)
        return 0;

        int l=0;int r=k-1; 
        vector<long long>v;
        for(int i=0;i<n;i++)
        {
            if(nums[i])
            {
                v.push_back(i);
            }
        }
        vector<long long>pre1(v.size()),pre2(v.size());
        pre1[0]=v[0];
        pre2[v.size()-1]=v[v.size()-1];
       

        for(int i=1;i<v.size();i++)
        {
            pre1[i]=pre1[i-1]+v[i];
        }
        for(int i=v.size()-2;i>=0;i--)
        {
            pre2[i]=pre2[i+1]+v[i];
        }
       
       

        
        long long ans=1e18;
        while(r<v.size())
        {
   
            int median1=-1;
            int median2=-1;
            
            median1=l+(r-l+1)/2;
            if(k%2==0)
            median2=l+(r-l)/2;

            cout<<median1<<endl;

            if(k%2)
            {
                long long range=k/2;
                long long tempans=0;
                tempans+=abs(pre1[median1+range]-pre1[median1]-v[median1]*range);
                tempans+=abs(pre2[median1-range]-pre2[median1]-v[median1]*range); 
                tempans-=((range)*(range+1));
                ans=min(ans,tempans);

            }
            else
            {
                long long range=k/2;
                long long tempans=0;
                tempans+=abs(pre1[median1+range-1]-pre1[median1]-v[median1]*(range-1));
                tempans+=abs(pre2[median1-range]-pre2[median1]-v[median1]*range);
                tempans-=((range)*(range+1)/2+((range)*(range-1))/2);
                ans=min(ans,tempans);
  

                long long tempans2=0;
                tempans2+=abs(pre1[median2+range]-pre1[median2]-v[median2]*(range));
                cout<<tempans<<" ";
                tempans2+=abs(pre2[median2-range+1]-pre2[median2]-v[median2]*(range-1));
                cout<<tempans<<" ";
                tempans2-=((range)*(range+1)/2+((range)*(range-1))/2);
                ans=min(ans,tempans2);

                
            }


            
            l++;
            r++;
        }
        return ans;


        
    }
};
154200