Ways to Split Array Into Good Subarrays

You are given a binary array nums.

A subarray of an array is good if it contains exactly one element with the value 1.

Return an integer denoting the number of ways to split the array nums into good subarrays. As the number may be too large, return it modulo 10^9 + 7.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1
Inputnums = [0,1,0,0,1]
Output3
There are 3 ways to split nums into good subarrays: [0,1] [0,0,1], [0,1,0] [0,1], and [0,1,0,0] [1].
Example 2
Inputnums = [0,1,0]
Output1
There is 1 way to split nums into good subarrays: [0,1,0].

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 1

Asked at 1 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate