hard

Minimum Number of K Consecutive Bit Flips

Find the fewest length-k flips needed to turn every 0 in a binary array into a 1.

1. Define the problem

Minimum Number of K Consecutive Bit Flips

Given a binary array nums and an integer k, you may pick any subarray of exactly k consecutive elements and flip every bit in it (0 becomes 1, 1 becomes 0), any number of times. Return the minimum number of flips needed to make every element 1, or -1 if it is impossible. Track how many active flips still affect the current position with a running counter instead of rewriting every element in the window.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ k ≤ nums.length
  • numsi is 0 or 1

Example

Inputnums = [0,1,0], k = 1
Output2

Explanation With k = 1 each flip toggles a single bit; flip index 0 and index 2 to get [1,1,1].

2. Know the words first

In plain terms

Subarray
A run of elements taken right out of the array as-is, next to each other in order — not values picked out from anywhere in the array.
3. Visualize the solution

A running counter tracks active flips

A running counter tracks active flips
StatusFlip

Position 0 is 0 with no active flips — start flip #1 here.

What happens in this step

i=0: currFlips = 0 (diff[0]=0)
(nums[0] + currFlips) % 2 = (0+0)%2 = 0  → still a 0, must flip here

flips: 0→1, currFlips: 0→1, diff[0+k]=diff[1]-- → marks flip #1 ends at i=1
Step 1 of 4

Steps to visualize

  1. Scan left to right, keeping a running count of flips still active at the current position.
  2. If the current bit, adjusted for active flips, is still a 0, a new flip must start here.
  3. Record the flip's end with a marker instead of rewriting all k elements of the window.
  4. When the marker position is reached later, subtract that flip back out of the running count.
  5. If a required flip would need to start past the point where k more elements fit, return -1.
  6. Add up every flip that was started for the final answer.
4. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

A running counter tracks active flips
StatusFlip

Position 0 is 0 with no active flips — start flip #1 here.

What happens in this step

i=0: currFlips = 0 (diff[0]=0)
(nums[0] + currFlips) % 2 = (0+0)%2 = 0  → still a 0, must flip here

flips: 0→1, currFlips: 0→1, diff[0+k]=diff[1]-- → marks flip #1 ends at i=1
Step 1 of 4
5. Solution

Solution

solution.tsTypeScript
function minKBitFlips(nums, k) {
  const n = nums.length;
  const diff = new Array(n + 1).fill(0);

  let flips = 0;
  let currFlips = 0;

  for (let i = 0; i < n; i++) {
    currFlips += diff[i];

    if ((nums[i] + currFlips) % 2 === 0) {
      if (i + k > n) return -1;
      flips++;
      currFlips++;
      diff[i + k]--;
    }
  }

  return flips;
}
Time
O(n)
Space
O(n)
6. Test cases

Test cases

InputExpectedCovers
nums = [0,1,0], k = 12Docstring example
nums = [1,1,0], k = 2-1Impossible — required flip runs past the array end
nums = [1,1,1], k = 20Already all 1s
nums = [0,0,0], k = 31k equals array length
nums = [0], k = 11Minimal single-element array needing a flip
nums = [1], k = 10Minimal single-element array already satisfied
nums = [0,0,0,1,0,1,1,0], k = 33Larger hand-verified case with overlapping flips
nums = [1,0,0,0], k = 4-1Impossible — not enough room left for the required flip