Skip to content

Commit 8530510

Browse files
authored
Merge pull request #2045 from Saipradyumnagoud/main
Added 995-minimum-number-of-k-consecutive-bit-flips
2 parents 54514c1 + 2f2730b commit 8530510

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
id: minimum-number-of-k-consecutive-bit-flips
3+
title: Minimum Number of K Consecutive Bit Flips
4+
sidebar_label: Minimum Number of K Consecutive Bit Flips
5+
tags:
6+
- Array
7+
- Greedy
8+
- Sliding Window
9+
description: "This document provides a solution for the Minimum Number of K Consecutive Bit Flips problem."
10+
---
11+
12+
## Problem Statement
13+
14+
You are given a binary array `nums` and an integer `k`.
15+
16+
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`.
17+
18+
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`.
19+
20+
A subarray is a contiguous part of an array.
21+
22+
**Example 1:**
23+
24+
Input: `nums = [0,1,0]`, `k = 1`
25+
26+
Output: `2`
27+
28+
Explanation: Flip `nums[0]`, then flip `nums[2]`.
29+
30+
**Example 2:**
31+
32+
Input: `nums = [1,1,0]`, `k = 2`
33+
34+
Output: `-1`
35+
36+
Explanation: No matter how we flip subarrays of size 2, we cannot make the array become `[1,1,1]`.
37+
38+
**Example 3:**
39+
40+
Input: `nums = [0,0,0,1,0,1,1,0]`, `k = 3`
41+
42+
Output: `3`
43+
44+
Explanation:
45+
- Flip `nums[0],nums[1],nums[2]`: `nums` becomes `[1,1,1,1,0,1,1,0]`
46+
- Flip `nums[4],nums[5],nums[6]`: `nums` becomes `[1,1,1,1,1,0,0,0]`
47+
- Flip `nums[5],nums[6],nums[7]`: `nums` becomes `[1,1,1,1,1,1,1,1]`
48+
49+
**Constraints:**
50+
51+
- `1 <= nums.length <= 10^5`
52+
- `1 <= k <= nums.length`
53+
54+
## Solutions
55+
56+
### Approach
57+
58+
To solve this problem, we can use a greedy approach with a sliding window to keep track of the flips. The main idea is to flip the bits only when necessary and use an auxiliary array to record the effect of flips at each position.
59+
60+
1. **Initialize Variables**:
61+
- `flipped` to track the cumulative effect of flips.
62+
- `res` to count the number of flips.
63+
- `isFlipped` array to record the flips at each index.
64+
65+
2. **Iterate through the Array**:
66+
- For each element, check if the current position is affected by a previous flip by using the `isFlipped` array.
67+
- If the current bit needs to be flipped (`flipped` == `nums[i]`), check if flipping is possible.
68+
- Record the flip and update the counters.
69+
70+
3. **Edge Cases**:
71+
- If it's not possible to flip because the subarray exceeds the array bounds, return `-1`.
72+
73+
### Java
74+
75+
```java
76+
class Solution {
77+
public int minKBitFlips(int[] nums, int k) {
78+
int n = nums.length, flipped = 0, res = 0;
79+
int[] isFlipped = new int[n];
80+
81+
for (int i = 0; i < nums.length; ++i) {
82+
if (i >= k)
83+
flipped ^= isFlipped[i - k];
84+
if (flipped == nums[i]) {
85+
if (i + k > nums.length)
86+
return -1;
87+
isFlipped[i] = 1;
88+
flipped ^= 1;
89+
res++;
90+
}
91+
}
92+
return res;
93+
}
94+
}
95+
```
96+
97+
### Python
98+
```python
99+
class Solution:
100+
def minKBitFlips(self, nums: List[int], k: int) -> int:
101+
n = len(nums)
102+
flipped = 0
103+
res = 0
104+
is_flipped = [0] * n
105+
106+
for i in range(n):
107+
if i >= k:
108+
flipped ^= is_flipped[i - k]
109+
if flipped == nums[i]:
110+
if i + k > n:
111+
return -1
112+
is_flipped[i] = 1
113+
flipped ^= 1
114+
res += 1
115+
116+
return res
117+
```

0 commit comments

Comments
 (0)