54. Max Consecutive Ones

EasyArrayArray

Given a binary array nums (containing only 0s and 1s), return the maximum number of consecutive 1s in the array.

Input: A binary integer array nums.

Output: Integer — maximum count of consecutive 1s.

Examples

Example 1
Input: [1,1,0,1,1,1]
Output: 3
Explanation: Runs: [1,1]=2, [1,1,1]=3. Max=3.
Example 2
Input: [1,0,1,1,0,1]
Output: 2
Explanation: Runs: [1]=1,[1,1]=2,[1]=1. Max=2.
Example 3
Input: [1,1,1,1]
Output: 4
Explanation: No zeros. All 4 consecutive. Max=4.

Constraints

Asked by

AccentureWiproCognizantBloombergGoogleAmazon
Solve this problem in the editor →