DSA problem
Max Consecutive Ones
Given a binary array nums, return the maximum number of consecutive 1s in the array.
You must scan the array and track how long the current run of consecutive ones is. Whenever you encounter a zero, the current run is broken and resets to zero. The answer is the longest run observed at any point during the traversal.
This is a classic single-pass counting problem and a common warm-up for sliding-window questions where zeros may be flipped.
Examples
Example 1
Input:
nums = [1,1,0,1,1,1]Output:
3Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
Example 2
Input:
nums = [1,0,1,1,0,1]Output:
2Explanation: The longest run of consecutive 1s has length 2.
Constraints
- 1 <= nums.length <= 10^5
- nums[i] is either 0 or 1.
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.