Max Consecutive Ones
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
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
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.
Expected Output