27. Minimum K Consecutive Bit Flips
β GFG solution to the Minimum K Consecutive Bit Flips problem: find minimum operations to convert all 0's to 1's using greedy approach with in-place marking. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a binary array arr[]
(containing only 0's and 1's) and an integer k
. In one operation, you can select a contiguous subarray of length k
and flip all its bits (i.e., change every 0 to 1 and every 1 to 0).
Your task is to find the minimum number of such operations required to make the entire array consist of only 1's. If it is not possible, return -1.
π Examples
Example 1
Input: arr = [1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1], k = 2
Output: 4
Explanation: 4 operations are required to convert all 0's to 1's.
Select subarray [2, 3] and flip all bits resulting array will be [1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1]
Select subarray [4, 5] and flip all bits resulting array will be [1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1]
Select subarray [5, 6] and flip all bits resulting array will be [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1]
Select subarray [6, 7] and flip all bits resulting array will be [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Example 2
Input: arr = [0, 0, 1, 1, 1, 0, 0], k = 3
Output: -1
Explanation: It is not possible to convert all elements to 1's by performing any number of operations.
π Constraints
$1 \le \text{arr.size()} \le 10^6$
$1 \le k \le \text{arr.size()}$
β
My Approach
The optimal approach uses a Greedy Algorithm with In-place Marking to efficiently track flip operations without using extra space:
Greedy Algorithm + In-place Marking
Core Insight:
Process the array from left to right (greedy approach).
When we encounter a 0 (after considering all previous flips), we must flip the subarray starting at that position.
Use the array itself to mark where flips have been applied.
Tracking Flip State:
Maintain a
flip
variable to track the current flip state.When we start a new flip operation, toggle the flip state.
When a previous flip operation ends (after k positions), toggle the flip state again.
In-place Marking:
Mark flip start positions by setting
arr[i] = 2
(since original values are 0 or 1).Check
arr[i - k] > 1
to detect when a flip operation ends.
Decision Making:
At each position, determine the effective value:
arr[i] ^ flip
.If the effective value is 0, we need to start a flip operation.
If starting a flip would go beyond array bounds, return -1.
Count Operations:
Increment flip counter each time we start a new flip operation.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. We make a single pass through the array, performing constant time operations at each position.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space (flip counter and flip state variable). The marking is done in-place within the input array.
π§βπ» Code (C++)
class Solution {
public:
int kBitFlips(vector<int>& a, int k) {
int n = a.size(), flips = 0, flip = 0;
for (int i = 0; i < n; i++) {
if (i >= k && a[i - k] > 1) flip ^= 1;
if ((a[i] ^ flip) == 0) {
if (i + k > n) return -1;
a[i] = 2;
flip ^= 1;
flips++;
}
}
return flips;
}
};
β Code (Java)
class Solution {
public int kBitFlips(int[] a, int k) {
int n = a.length, flips = 0, flip = 0;
for (int i = 0; i < n; i++) {
if (i >= k && a[i - k] > 1) flip ^= 1;
if ((a[i] ^ flip) == 0) {
if (i + k > n) return -1;
a[i] = 2;
flip ^= 1;
flips++;
}
}
return flips;
}
}
π Code (Python)
class Solution:
def kBitFlips(self, a, k):
n, flips, flip = len(a), 0, 0
for i in range(n):
if i >= k and a[i - k] > 1: flip ^= 1
if (a[i] ^ flip) == 0:
if i + k > n: return -1
a[i] = 2
flip ^= 1
flips += 1
return flips
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated