<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) {
        long long result = LLONG_MAX; // Use long long for large results
        if(k==100000) return 0;
        vector<int> total; // To store indices of all 1s
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] == 1) {
                total.push_back(i);
            }
        }
        int n = total.size();
        vector<long long> s = {0}; // To store the prefix sum of the indices of 1s
        for (int i = 0; i < n; i++) {
            s.push_back(s.back() + total[i]);
        }
        for (int i = k / 2; i < n - (k - 1) / 2; i++) {
            long long current = k / 2 * total[i] - (s[i] - s[i - k / 2]) - k / 2 * (k / 2 + 1) / 2; // Calculate distances for left side
            //k / 2 * total[i]:

//This term represents the total "target cost" if all k / 2 1s were to move to the position total[i].
//(s[i] - s[i - k / 2]):

//This term gives the sum of the indices of the k / 2 1s to the left of position i. It represents the current total position of these 1s.
//k / 2 * (k / 2 + 1) / 2:

//This term calculates the cost of aligning k / 2 1s around the median. It accounts for the sum of distances required to move k / 2 1s from their original positions to the median position.
            current += (s[i + 1 + (k - 1) / 2] - s[i + 1]) - (k - 1) / 2 * total[i] - (k - 1) / 2 * ((k - 1) / 2 + 1) / 2; // Calculate distances for right side
            result = min(result, current);
        }
        return result;
    }
};

145900