Skip to content

Practice Minimum Number of K Consecutive Bit Flips

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#995
LeetCode Problem

Minimum Number of K Consecutive Bit Flips

Problem Statement

You are given a binary array nums and an integer k. A k-bit flip is choosing a subarray of length k from nums and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of k-bit flips required so that there is no 0 in the array. If it is not possible, return -1. A subarray is a contiguous part of an array.

Source: Minimum Number of K Consecutive Bit Flips on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [0,1,0], k = 1
Output
2

# Example 2

Input
nums = [1,1,0], k = 2
Output
-1

# Example 3

Input
nums = [0,0,0,1,0,1,1,0], k = 3
Output
3

Constraints

1 <= nums.length <= 10^5
1 <= k <= nums.length

Related LeetCode Problems

Practice more problems similar to Minimum Number of K Consecutive Bit Flips.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.