Arrays and Hashing Array basics
53. Max Consecutive Ones
Easy
You are given an array nums in which every element is 0 or 1. Return the length of its longest run of
1s.
A run of 1s is a block of 1s that sit next to each other, with no 0 between them. If nums holds no 1s
at all, return 0.
Example 1
Input: nums = [1, 1, 0, 1, 1, 1, 0, 1]
Output:
3Explanation: The 1s form three runs: two at the start, three in the middle and one at the end. The longest run has length 3.
Example 2
Input: nums = [0, 0, 0, 0]
Output:
0Example 3
Input: nums = [1, 0, 1, 1, 1, 1]
Output:
4Explanation: The longest run is the last four elements. It reaches the end of the array, so no 0 comes after it.
Check your understanding
Work out the answer for this input before you write any code.
nums = [0, 1, 1, 0, 1, 1, 1, 1]
Constraints
1 ≤ nums.length ≤ 105nums[i]is0or1
Clarifying questions
What you would ask an interviewer before writing any code.