Find max consecutive 1s if you can flip at most one 0.
Input: nums=[1,0,1,1,0] → Output: 4
Input: nums=[1,0,1,1,0,1] → Output: 4
Sliding window tracking number of zeros flipped (at most 1).
- Track left pointer and count of zeros in window
- When zeros > 1: advance left, decrement zeros if left was 0
- Track max window size
public int findMaxConsecutiveOnes(int[] nums) {
int left = 0, zeros = 0, max = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] == 0) zeros++;
while (zeros > 1) { if (nums[left++] == 0) zeros--; }
max = Math.max(max, right - left + 1);
}
return max;
}
- Time Complexity: O(n)
- Space Complexity: O(1)