Menu
CoddyTech

Max Consecutive Ones

You get an array nums in which every value is 0 or 1. A run is a stretch of 1s that sit next to each other with no 0 between them. Return the length of the longest run, or 0 if the array holds no 1 at all.

Function

findMaxConsecutiveOnes(nums: integer-array) → integer
numsinteger-array
an array of 0s and 1s
Returnsinteger
the length of the longest run of consecutive 1s

Constraints

  • 1 ≤ nums.length ≤ 2 × 104
  • Every nums[i] is 0 or 1.

Examples

Input
nums = [1, 1, 0, 1, 1, 1, 0, 1]
Output
3
Explanation
The 1s form three runs: indexes 0 to 1 (length 2), 3 to 5 (length 3) and index 7 alone (length 1). The longest has length 3.

lock icon+14 hidden tests on Submit

challenge icon

Follow-up

What if you may flip up to k zeros to ones? How long can the longest run of 1s get, and can you still find it in one pass?

Reset code
def findMaxConsecutiveOnes(nums):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

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

Expected

3