Ctrl K

Ones After One Removal

Medium
You are given an array bits of zeros and ones. Remove exactly one element of your choice. Return the greatest length of a run of consecutive ones the remaining array can have. If no ones are left, return 0.
Example 1
bits=[1, 1, 0, 1, 1, 1, 0, 1] → 5

Remove the first zero to get [1, 1, 1, 1, 1, 0, 1]: five ones in a row.

Example 2
bits=[1, 1, 1, 1] → 3

Removing an element is mandatory, so three ones are left.

Example 3
bits=[0, 0] → 0

There are no ones at all.

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 problemsEvenly Stepped Stretches MediumLongest Run of Ones With One Fix MediumLongest Shared Run MediumSawtooth Readings MediumTwo Separate Windows Medium
loading Python…
Input
bits=[1, 1, 0, 1, 1, 1, 0, 1]
Expected
5

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