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

#define ll int
class Solution {
public:
    ll solve(ll l , ll r, vector<ll>one,int prefix[], int suffix[], int pre[], int suff[]){
           long long  ans = 1e16;
           for(int i = l;i<one.size()-r;i++){
            ll curr = one[i];
            ll left = one[i-l];
            ll right = one[i+r];
            ll j = i;
            ll val = (suffix[left]-suffix[curr]-l*suff[curr])+(prefix[right]-prefix[curr]-r*pre[curr]);
            ans = min(ans,(long long )val);
           }
           return ans ;
    }
    int minMoves(vector<int>& nums, int k) {
        vector<ll>v = nums;
        ll n = v.size();
        int pre[n];
        int prefix[n];
        int suff[n];
        int suffix[n];
        vector<ll>one;
        memset(pre,0,sizeof(pre));
        memset(prefix,0,sizeof(prefix));
        memset(suff,0,sizeof(suff));
        memset(suffix,0,sizeof(suffix));
        pre[0]=(v[0]==0);
        if(v[0]){
            one.push_back(0);
        }
        for(int i = 1;i<n;i++){
            pre[i]= pre[i-1]+(v[i]==0);
            if(v[i]){
                prefix[i] = pre[i];
                one.push_back(i);
            }
        }
        suff[n-1]= (!v[n-1]);
        for(int i = n-2;i>=0;i--){
             suff[i]= suff[i+1]+(v[i]==0);
            if(v[i]){
                suffix[i] = suff[i];
            }
        }
        for(int i = 1;i<n;i++){
            prefix[i]+=prefix[i-1];
        }
        for(int i = n-2;i>=0;i--){
            suffix[i]+=suffix[i+1];
        }
        k--;
        if(k==0){
            return 0;
        }
        if(k&1){
           ll x = k/2;
           return min(solve(x+1,x,one,prefix,suffix,pre,suff),solve(x,x+1,one,prefix,suffix,pre,suff));
        }
        else{
             return solve(k/2,k/2,one,prefix,suffix,pre,suff);
        }
        return -1;
    }
};
146000