<p>You are given a binary array <code>nums</code> and an integer <code>k</code>.</p>

<p>A <strong>k-bit flip</strong> is choosing a <strong>subarray</strong> of length <code>k</code> from <code>nums</code> and simultaneously changing every <code>0</code> in the subarray to <code>1</code>, and every <code>1</code> in the subarray to <code>0</code>.</p>

<p>Return <em>the minimum number of <strong>k-bit flips</strong> required so that there is no </em><code>0</code><em> in the array</em>. If it is not possible, return <code>-1</code>.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,0], k = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> Flip nums[0], then flip nums[2].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,0], k = 2
<strong>Output:</strong> -1
<strong>Explanation:</strong> No matter how we flip subarrays of size 2, we cannot make the array become [1,1,1].
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,0,0,1,0,1,1,0], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> 
Flip nums[0],nums[1],nums[2]: nums becomes [1,1,1,1,0,1,1,0]
Flip nums[4],nums[5],nums[6]: nums becomes [1,1,1,1,1,0,0,0]
Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
</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>1 &lt;= k &lt;= nums.length</code></li>
</ul>

static const bool Booster = [](){
    std::ios_base::sync_with_stdio(false);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
    return true;
}();

inline bool isDigit(char c) {
    return (c >= '0') && (c <= '9');
}

constexpr int PROBLEM_SIZE = 100'000;
static std::bitset<PROBLEM_SIZE> isFlipped;

int parse_input_and_solve(const std::string& nums, const int k) {
    const int N = (nums.size() - 1) / 2;

    int currentFlips = 0;   // Tracks the current number of flips
    int totalFlips = 0;     // Tracks the total number of flips

    int i = 0;
    for (char c : nums) {
        if (!isDigit(c)) {
            continue;
        }
        const int n = c - '0';
        isFlipped[i] = 0;
        
        // If the window slides out of the range and the leftmost element
        // is marked as flipped (2), decrement currentFlips
        if (i >= k && isFlipped.test(i - k)) {
            currentFlips--;
        }

        // Check if the current bit needs to be flipped
        if ((currentFlips % 2) == n) {
            // If flipping would exceed array bounds, return -1
            if (i + k > N) {
                return -1;
            }
            // Mark the current bit as flipped
            isFlipped.set(i);
            currentFlips++;
            totalFlips++;
        }
        ++i;
    }
    return totalFlips;
}

static bool Solve = [](){
    std::ofstream out("user.out");
    std::string nums_, k_;
    while (std::getline(std::cin, nums_) && std::getline(std::cin, k_)) {
        const int k = std::stoi(k_);
        out << parse_input_and_solve(nums_, k) << "\n";
    } 
    out.flush();
    exit(0);
    return true;
}();

class Solution {
public:
    int minKBitFlips(std::vector<int>& nums, int k) {
        const int N = nums.size();

        // Tracks the current number of flips
        int currentFlips = 0;
        // Tracks the total number of flips
        int totalFlips = 0;

        for (int i = 0; i < N; ++i) {
            // If the window slides out of the range and the leftmost element
            // is marked as flipped (2), decrement currentFlips
            if (i >= k && nums[i - k] == 2) {
                currentFlips--;
            }

            // Check if the current bit needs to be flipped
            if ((currentFlips % 2) == nums[i]) {
                // If flipping would exceed array bounds, return -1
                if (i + k > N) {
                    return -1;
                }
                // Mark the current bit as flipped
                nums[i] = 2;
                currentFlips++;
                totalFlips++;
            }
        }

        return totalFlips;
    }
};
8200