Standard Bar Sliding Window · Medium Problems O(n) · O(1)
A gas pipeline is monitored by sensors that report either 1 (healthy) or 0 (faulty), and the maintenance crew carries a budget of k replacement kits. Flipping a faulty sensor makes it healthy, and control wants the longest stretch of consecutive healthy readings achievable with at most k flips. The planner slides a frame over the sensor log, expelling older faults whenever the running fault count exceeds the budget.
Input: A binary array nums of sensor readings and an integer k, the flip budget.
Output: Return the length of the longest subarray containing at most k zeros.
1 <= nums.length <= 10^5nums[i] is 0 or 10 <= k <= nums.lengthInput: {"nums":[1,1,1,0,0,0,1,1,1,1,0],"k":2}
Output: 6
Flipping two zeros welds the tail into six consecutive healthy readings.
Input: {"nums":[1,0,1,1,0,1],"k":1}
Output: 4
One flip covers either fault, but both zeros can never sit inside the same frame.