ExamAdda Logo

Max Consecutive Ones

Easy

Given a binary array nums, return the maximum number of consecutive 1s in the array.

Example 1

Input

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

Output

3

Explanation

The array contains two consecutive 1s at the beginning and three consecutive 1s at the end. The maximum count is 3.

Example 2

Input

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

Output

2

Explanation

The longest consecutive sequence of 1s contains two elements.

Constraints

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

Hints:

Hint 1

Keep track of the current consecutive count of 1s and reset it when a 0 is encountered.

Hint 2

When a 0 is encountered, reset the current count to 0 and update the maximum count.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 17, 2026

Last updated

Aug 18, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output