Ctrl K

All-Zero Stretches

Medium
A sensor logged its readings in an integer array readings. Count the non-empty stretches (runs of consecutive elements) that consist entirely of zeros. Stretches at different positions count separately, even if they have the same length.
Example 1
readings=[0, 0, 0, 5, 0] → 7

The run of three zeros gives 3 + 2 + 1 = 6 stretches, and the lone zero at the end adds one more: 7 in total.

Example 2
readings=[4, -1, 0, 0] → 3

The zeros sit together at the end: two stretches of length 1 and one of length 2.

Example 3
readings=[7, 8] → 0

There are no zeros at all.

Constraints
  • 1 ≤ len(readings) ≤ 10⁵
  • -10⁹ ≤ readings[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTurn a Square Clockwise MediumPostfix Calculator MediumRoll an Array to the Right MediumPrimes Below a Bound MediumNth Number Built From Given Primes Medium
loading Python…
Input
readings=[0, 0, 0, 5, 0]
Expected
7

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