Ctrl K

Cuts With One Flag per Piece

Medium
You get an array bits of zeros and ones. Cut it into non-empty contiguous pieces so that every piece contains **exactly one** 1. Return the number of ways to do it. If the array has no 1s, the answer is 0. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
bits=[1, 0, 0, 1, 0, 1] → 6

Between the first and second 1 the cut can go in 3 places, between the second and third in 2. That gives 3 · 2 = 6 ways.

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

There is a single 1, so the only option is the whole array as one piece: 1 way.

Example 3
bits=[0, 0, 0] → 0
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 problemsNth Number Built From Given Primes MediumChain of Multiples MediumBest Rotation for a Weighted Sum MediumDuel for the End Cards MediumBiggest Division Chain Medium
loading Python…
Input
bits=[1, 0, 0, 1, 0, 1]
Expected
6

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