Ctrl K

Longest Run of Ones

Easy
You are given an array bits where every element is either 0 or 1. Find the longest stretch of consecutive ones and return its length. If the array contains no ones, return 0.
Example 1
bits=[0, 1, 1, 0, 1, 1, 1, 1, 0] → 4

There is a run of two ones and a run of four; the longest is 4.

Example 2
bits=[0, 0, 0] → 0

There are no ones at all, so the answer is 0.

Example 3
bits=[1, 0, 1] → 1
Constraints
  • 1 ≤ len(bits) ≤ 10⁵
  • bits[i] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
bits=[0, 1, 1, 0, 1, 1, 1, 1, 0]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.