Problem

Given a binary array nums, return the maximum number of consecutive1 ’ s in the array.

Examples

Example 1

1
2
3
Input: nums = [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.

Example 2

1
2
Input: nums = [1,0,1,1,0,1]
Output: 2

Constraints

  • 1 <= nums.length <= 10^5
  • nums[i] is either 0 or 1.

Solution

Method 1 – Single Pass Linear Scan

Intuition

We want to find the longest sequence of consecutive 1s in the array. By scanning the array once and counting the current streak of 1s, we can keep track of the maximum streak found so far.

Approach

  1. Initialize two variables: ans for the maximum streak and cnt for the current streak.
  2. Iterate through the array:
    • If the current element is 1, increment cnt and update ans if cnt is greater.
    • If the current element is 0, reset cnt to 0.
  3. Return ans after the loop.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int ans = 0, cnt = 0;
        for (int x : nums) {
            if (x == 1) cnt++;
            else cnt = 0;
            ans = max(ans, cnt);
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
func findMaxConsecutiveOnes(nums []int) int {
    ans, cnt := 0, 0
    for _, x := range nums {
        if x == 1 {
            cnt++
            if cnt > ans {
                ans = cnt
            }
        } else {
            cnt = 0
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int ans = 0, cnt = 0;
        for (int x : nums) {
            if (x == 1) cnt++;
            else cnt = 0;
            ans = Math.max(ans, cnt);
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    fun findMaxConsecutiveOnes(nums: IntArray): Int {
        var ans = 0
        var cnt = 0
        for (x in nums) {
            if (x == 1) cnt++
            else cnt = 0
            ans = maxOf(ans, cnt)
        }
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
    def findMaxConsecutiveOnes(self, nums: list[int]) -> int:
        ans = cnt = 0
        for x in nums:
            if x == 1:
                cnt += 1
                ans = max(ans, cnt)
            else:
                cnt = 0
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
impl Solution {
    pub fn find_max_consecutive_ones(nums: Vec<i32>) -> i32 {
        let mut ans = 0;
        let mut cnt = 0;
        for x in nums {
            if x == 1 {
                cnt += 1;
                if cnt > ans {
                    ans = cnt;
                }
            } else {
                cnt = 0;
            }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    findMaxConsecutiveOnes(nums: number[]): number {
        let ans = 0, cnt = 0;
        for (const x of nums) {
            if (x === 1) {
                cnt++;
                ans = Math.max(ans, cnt);
            } else {
                cnt = 0;
            }
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of the array, since we scan the array once.
  • 🧺 Space complexity: O(1), as only a few variables are used.