<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:
    def minMoves(self, nums: List[int], k: int) -> int:
        one_ind = []
        for i, n in enumerate(nums):
            if n == 1: one_ind.append(i)
        print(one_ind)

        left, right = 0, k-1

        mid = (left + right)//2
        l = r = one_ind[mid]
        shifts = 0
        for i in range(left,right+1):
            pos = one_ind[i]
            if i < mid:
                shifts += l - pos - 1
                l -= 1
            elif i > mid:
                shifts += pos - r - 1
                r += 1
            
        min_shifts = shifts
        print(left, right, shifts)
        left += 1
        mid += 1
        right += 1

        lmargin = (k - 1) // 2
        rmargin = k // 2

        while right < len(one_ind):
            sub = one_ind[mid-1] - one_ind[left - 1] - lmargin
            sub2 = (right - mid) * (one_ind[mid] - one_ind[mid-1] - 1)
            add = (mid - left) * (one_ind[mid] - one_ind[mid-1] - 1)
            add2 = one_ind[right] - one_ind[mid] - rmargin
            shifts += add + add2 - sub - sub2
            print("s/a:", sub, sub2, add, add2)
            print(left, right, shifts)
            min_shifts = min(min_shifts, shifts)

            left += 1
            mid += 1
            right += 1
        
        return min_shifts

23900