Skip to main content
Back to problems
Leetcode
Easy
Arrays
Max Consecutive Ones

Find the longest run of 1s in a binary array.

Acceptance 84%
Problem Statement

You are given a binary array nums containing only 0s and 1s. Return the length of the longest contiguous subarray that contains only 1s.

A contiguous subarray must consist of consecutive elements from the original array.

Input Format

  • An integer array nums.
  • Each element is either 0 or 1.

Output Format

  • Return a single integer: the maximum number of consecutive 1s appearing in nums.

Constraints

  • 1 <= nums.length
  • nums[i] is either 0 or 1.
  • Solve in linear time if possible.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1,1,0,1,1,1]

Output

3

Explanation

The longest contiguous block of 1s is [1,1,1], which has length 3.

Example 2

Input

nums = [1,0,1,1,0,1]

Output

2

Explanation

There are multiple blocks of 1s, and the longest one has length 2.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.