Consecutive Ones III
HardAcc. 81.4%
+45 XP 20
Optimized Flipping
Given a binary array and an integer k, find the longest subarray containing only 1s after flipping at most k 0s.
The Assignment
Your function receives nums and k.
- Sliding window: expand
right. - If you hit a 0, decrement
k. - If
k < 0, moveleftforward and if you move past a 0, incrementk. - Print the maximum window size.
01EXAMPLE 1
Input
nums=[1,1,1,0,0,0,1,1,1,1,0], k=2Output
6Explanation: Flipping two 0s at idx 5, 10 allows a streak of 6.
Constraints
- Single pass (O(n)).
ArraysSliding Window
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
nums[1,1,1,0,0,0,1,1,1,1,0]
k2
Expected Output
6
Click RUN to test your solution