Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Sliding Window

Minimum K Consecutive Bit Flips

Hard · Sliding Window · Asked at Google, Amazon

A k-flip chooses k consecutive elements of the binary array and flips every one of them (0 ↔ 1). Print the minimum number of k-flips needed to make every element 1, or -1 if it is impossible.

Input

First line: n k. Second line: n values (0 or 1).

Output

The minimum number of flips, or -1.

Constraints

Go left to right. The leftmost 0 can only be fixed by a flip that starts exactly there, so the greedy is forced. Track how many active flips cover the current index with a queue (or a difference array) instead of flipping k elements each time — O(n).

Example 1

Input

3 1
0 1 0

Output

2

Example 2

Input

3 2
1 1 0

Output

-1

Example 3

Input

8 3
0 0 0 1 0 1 1 0

Output

3

Hints

  1. The leftmost 0 can only be fixed by a flip that starts exactly at it.
  2. So scan left to right and flip whenever the current bit is effectively 0.
  3. Do not flip k elements each time — count how many active flips cover the current index.

Approach

Keep active = number of flips covering index i, and a difference array ends[] marking where each flip stops covering. At each i, subtract ends[i] from active; the current bit is a[i] flipped active times, i.e. 0 when (a[i] + active) is even. Then you must start a flip at i: if i + k > n it does not fit — print −1; otherwise count it, increase active and mark ends[i + k]. Each index is handled in O(1).

Complexity: O(n) time · O(n) space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<int> a(n), ends(n + 1, 0);
    for (auto &x : a) cin >> x;
    int active = 0, flips = 0;
    for (int i = 0; i < n; i++) {
        active -= ends[i];
        if ((a[i] + active) % 2 == 0) {
            if (i + k > n) {
                cout << -1 << "\n";
                return 0;
            }
            flips++;
            active++;
            ends[i + k]++;
        }
    }
    cout << flips << "\n";
    return 0;
}

Python solution

import sys

data = list(map(int, sys.stdin.read().split()))
n, k = data[0], data[1]
a = data[2 : 2 + n]
ends = [0] * (n + 1)
active = flips = 0
answer = None
for i in range(n):
    active -= ends[i]
    if (a[i] + active) % 2 == 0:
        if i + k > n:
            answer = -1
            break
        flips += 1
        active += 1
        ends[i + k] += 1
print(flips if answer is None else answer)

Java solution

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        in.nextToken();
        int k = (int) in.nval;
        int[] a = new int[n], ends = new int[n + 1];
        for (int i = 0; i < n; i++) {
            in.nextToken();
            a[i] = (int) in.nval;
        }
        int active = 0, flips = 0;
        for (int i = 0; i < n; i++) {
            active -= ends[i];
            if ((a[i] + active) % 2 == 0) {
                if (i + k > n) {
                    System.out.println(-1);
                    return;
                }
                flips++;
                active++;
                ends[i + k]++;
            }
        }
        System.out.println(flips);
    }
}

Solve Minimum K Consecutive Bit Flips online · All practice problems