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

class Solution {
public:
    int minMoves(vector<int>& nums, int k) {
        // Base case: if k == 1, we already have 1 consecutive 1, so no swaps are needed
        if (k == 1) {
            return 0;
        }

        int n = nums.size();  // Get the size of the input array `nums`

        // Step 1: Store the indices of all the 1's in the `index` vector
        vector<int> index;
        for (int i = 0; i < n; i++) {
            if (nums[i]) {
                index.push_back(i);  // Store the position of each 1 in the array
            }
        }

        // Step 2: Calculate the prefix sum of the indices array
        // This helps in efficient range sum calculations during the sliding window process
        vector<ll> prefix;
        ll sum = 0;
        for (int x : index) {
            sum += x;
            prefix.push_back(sum);  // Store cumulative sum of indices
        }

        // Step 3: Sliding window to check all subarrays of size `k` in the `index` array
        // Variables `i` and `j` represent the start and end of the sliding window
        int i = 0, j = k - 1;
        
        // Calculate the radius, which determines how far we need to shift the 1's to group them together
        ll radius = (k % 2 ? k / 2 : (k - 2) / 2);  // Radius is half of `k` if `k` is odd, else adjusted for even

        ll ans = 1e15;  // Initialize the result with a large value, as we will minimize it

        // Step 4: Sliding window logic
        while (j < index.size()) {
            ll temp = 0;  // This variable will store the number of swaps for the current window

            // Case 1: `k` is odd
            if (k % 2) {
                int pivot = (i + j) / 2;  // Find the median index (pivot) in the current window
                // Calculate the number of swaps required to group the 1's around the pivot
                temp = prefix[j] - prefix[pivot] -  // Sum of right half relative to pivot
                       ((pivot > 0 ? prefix[pivot - 1] : 0) - (i > 0 ? prefix[i - 1] : 0)) -  // Sum of left half relative to pivot
                       radius * (radius + 1);  // Adjust for the radius difference (distance from pivot)
            }
            // Case 2: `k` is even
            else {
                int pivot = (i + j) / 2;  // Find the median index (pivot) in the current window
                // Calculate the number of swaps required, taking into account that the center is between two indices
                temp = prefix[j] - prefix[pivot] -  // Sum of right half relative to pivot
                       ((pivot > 0 ? prefix[pivot - 1] : 0) - (i > 0 ? prefix[i - 1] : 0)) -  // Sum of left half relative to pivot
                       index[pivot] -  // Subtract the pivot position itself
                       radius * (radius + 1) - (radius + 1);  // Adjust for the radius difference (distance from pivot)
            }

            // Step 5: Minimize the number of swaps
            ans = min(ans, temp);

            // Move the sliding window forward
            i++;
            j++;
        }

        // Return the minimum number of swaps needed
        return ans;
    }
};

148800