Skip to content

Commit 3c9df0b

Browse files
committed
Add Max Consecutive Ones Variant task
1 parent dbcc9f7 commit 3c9df0b

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Max Consequitve Ones Variant
2+
3+
## Problem Description
4+
5+
Given a binary array nums, find the maximum length of a consecutive subarray consisting only of 1s after removing exactly one element (either 0 or 1).
6+
7+
**Example 1:**
8+
9+
Input: `[0, 0]`
10+
Output: `0`
11+
Explanation: Removing any element leaves `[0]` (no `1`s).
12+
13+
**Example 2:**
14+
15+
Input: `[0, 1]`
16+
Output: `1`
17+
Explanation: Remove `0`: `[1]` → sequence of `1` has length `1`.
18+
19+
**Constraints:**
20+
21+
* `1 <= len(nums) <= 10^5`
22+
* `nums[i]` is `0` or `1`
23+
24+
## Solution
25+
26+
```python
27+
def max_ones_after_remove_one(nums: list[int]) -> int:
28+
left = 0
29+
30+
zero_position = -1
31+
zero_counter = 0
32+
max_counter = 0
33+
34+
for right in range(len(nums)):
35+
if nums[right] == 0:
36+
if zero_counter == 1:
37+
left = zero_position + 1
38+
zero_counter -= 1
39+
zero_counter += 1
40+
zero_position = right
41+
max_counter = max(max_counter, right - left + 1 - zero_counter)
42+
43+
return max_counter
44+
```
45+
46+
* **Time Complexity:** O(n). The algorithm processes each element exactly once.
47+
* **Space Complexity:** O(1), as only a few variables are used for tracking.
48+
49+
## Explanation of the Solution
50+
51+
The problem requires finding the maximum length of a consecutive subarray consisting only of 1s after removing exactly one element (either `0` or `1`) from the original array. The solution uses a sliding window approach to efficiently track the longest valid subarray.
52+
Key Ideas:
53+
54+
1. **Sliding Window with At Most One Zero:**
55+
* The window `[left, right]` can contain at most one `0`. If a second `0` is encountered, the window is adjusted to exclude the first `0`.
56+
* This ensures that after removing one `0`, the remaining subarray consists of all 1s.
57+
2. **Tracking Zero Position:**
58+
* `zero_position` keeps track of the index of the most recent `0` encountered.
59+
* `zero_counter` counts the number of `0`s in the current window (either `0` or `1`).
60+
3. **Window Adjustment:**
61+
* If a new `0` is encountered when `zero_counter == 1`, the left boundary of the window (left) is moved to `zero_position + 1` (skipping the first `0`).
62+
* This allows the window to include the new `0` while maintaining at most one `0` in the window.
63+
4. **Calculating Maximum Length:**
64+
* The length of the current valid subarray is `right - left + 1 - zero_counter`. Since `zero_counter` is at most `1`, subtracting it accounts for the one allowed removal.
65+
* The maximum length encountered during the iteration is stored in `max_counter`.
66+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def max_ones_after_remove_one(nums: list[int]) -> int:
2+
"""
3+
Finds the maximum length of a consecutive subarray of 1s after removing exactly one element (0 or 1).
4+
5+
:param nums: A list of integers containing only 0s and 1s.
6+
:return: The maximum length of consecutive 1s achievable by deleting exactly one element from the list.
7+
"""
8+
left = 0
9+
10+
zero_position = -1
11+
zero_counter = 0
12+
max_counter = 0
13+
14+
for right in range(len(nums)):
15+
if nums[right] == 0:
16+
if zero_counter == 1:
17+
left = zero_position + 1
18+
zero_counter -= 1
19+
zero_counter += 1
20+
zero_position = right
21+
max_counter = max(max_counter, right - left + 1 - zero_counter)
22+
23+
return max_counter
24+
25+
26+
assert max_ones_after_remove_one([0, 0]) == 0, 'Test 1 failed'
27+
assert max_ones_after_remove_one([0, 1]) == 1, 'Test 2 failed'
28+
assert max_ones_after_remove_one([1, 0, 1]) == 2, 'Test 3 failed'
29+
assert max_ones_after_remove_one([1, 1, 1]) == 3, 'Test 4 failed'
30+
assert max_ones_after_remove_one([1, 1, 0, 1, 1]) == 4, 'Test 5 failed'
31+
assert max_ones_after_remove_one([1, 1, 1, 0, 1, 1, 0]) == 5, 'Test 6 failed'
32+
assert max_ones_after_remove_one([1, 1, 0, 1, 0, 1]) == 3, 'Test 7 failed'
33+
assert max_ones_after_remove_one([1, 1, 0, 0, 1, 0, 1]) == 2, 'Test 8 failed'
34+
assert max_ones_after_remove_one([1, 0, 1, 0, 1, 1, 1]) == 4, 'Test 9 failed'
35+
assert max_ones_after_remove_one([0, 0, 0, 0]) == 0, 'Test 10 failed'
36+
assert max_ones_after_remove_one([1, 0, 1, 1, 0]) == 3, 'Test 11 failed'

0 commit comments

Comments
 (0)