dsa · easy

Max Consecutive Ones

nums is a binary array: every entry is 0 or 1. Return the length of the longest consecutive run of 1s. If there is no 1, return 0.

Example

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

Runs of ones: a single 1 at the start (length 1), then three 1s in the middle (length 3), then a trailing 1 (length 1). The longest is 3.

nums = [0, 0, 0] has no ones → 0.

nums = [1]1.

## Arguments - nums — binary array; each entry is 0 or 1

Constraints

1 <= nums.length <= 10^5 nums[i] is 0 or 1 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[1,0,1,1,1,0,1]

Expected:
3

Example 2

Input:
[0,0,0]

Expected:
0

Example 3

Input:
[1]

Expected:
1

Open in the Dojo editor