<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) {
        vector<int> ones;
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i] == 1)
                ones.push_back(i);
        }
        
        //Explore Each window of size k using sliding window of fixed length k.
        // If k = 5 and have values [0,2,4,5,7]
        //We need to change all values to median and again change it to consecutive.
        //[0,2,4,5,7]  --->(Step 1)   [4,4,4,4,4]  ---->(Step 2) [2,3,4,5,6]
        
        //Step 1: Is simular to Minimum moves to make all array elements equal.
        //(4-0) + (4-2) + (5-4) + (7 - 4) ===> (5 + 7) - (0 + 2) ==> 
        //Which is difference in sum of right and sum of left.
        
        //Step 2: Change [4,4,4,4,4] to consecutive [2,3,4,5,6]
        //We use radius = k/2
        //(k + k-1 + k-2 + ... + 0 + 1 + 2 + 3 +... + k-1 + k+1)
        //(1 + 2 + 3 + .... + k) * 2 => k(k + 1)/2 * 2 ==> k(k+1)
        //right - left - k(k+1) for k is odd
        //right - left - i - k(k+1) - (k+1) for k is even
         int n=ones.size();
          vector<long long int> preSum(n, 0);
         preSum[0] = ones[0];
        for(int i=1;i<n;i++)
            preSum[i] = preSum[i-1] + ones[i];
        int i=0,j=0;
       long long int ans  = LLONG_MAX;
        while(j < n){
            if(j-i+1 == k){
                // cout<<"Hello"<<endl;
                int mid = (j + i)/2;
                long long int right = preSum[j] - preSum[mid];
                long long int left = (mid-1 < 0 ? 0 : preSum[mid - 1]) - (i-1 < 0 ? 0 : preSum[i-1]);
             // cout<<mid<<" "<<left<<" "<<right<<endl;
                int radius = (k-1)/2;
                long long int p = (long long int)radius*(radius + 1);
                long long int sum = right - left - p;
                if(k % 2 == 0){
                    sum -= (radius + 1);
                    sum -= ones[mid];
                }
               // cout<<sum<<endl;
                ans = min(ans, sum);
                i++;
            }
            j++;
        }
        return ans;
    }
};
139200