> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/two-pointer/sliding-window/487.-max-consecutive-ones-ii.md).

# 487. Max Consecutive Ones II

```
T: o(n)
S: O(1)
```

````java
```java
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int left = 0;
        int right = 0;
        int zeroIdx = -1;
        int result = 0;
        while (right < nums.length) {
            int rightInt = nums[right];
            right++;
            if (rightInt == 0) {
                if (zeroIdx == -1) {
                    zeroIdx = right-1;
                } else {
                    left = zeroIdx+1;
                    zeroIdx = right-1;
                }
            }
            result = Math.max(result, right - left);
        }
        return result;
    }
}

/**
sliding win
T: o(n)
S: O(1)

if win only has one 0 -> it's a 1

zeroidx = 1

1,0,1,1,0


0,0, 0

idea, update zero's postion,
and left = zeroIdx+1; so our win only has 0 zero (flip to one)
       
 */
```
````
