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

# Max Consecutive Ones I

Sliding Window

## Problem

Given a binary array, find the maximum number of consecutive 1s in this array.

{% hint style="info" %}
For example:

```
Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last
three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
```

{% endhint %}

### Thought Process

* Apply sliding window technique. This is an easy problem

* When a zero is come across, update start index&#x20;

## Solution

```
class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        start = 0
        maxL = 0
        for i in range(len(nums)):
            if nums[i] == 0:
                start = i+1
            else:
                maxL = max(maxL, i-start+1)
                
        return maxL
```

## Key Points

* When an element is a zero, update starting position of window to be the next index

## Time Complexity

* **Time:** $$O(n)$$&#x20;
* **Space:** $$O(1)$$&#x20;
