Binary Subarrays With Sum

Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum goal.

A subarray is a contiguous part of the array.

Example 1
Inputnums = [1,0,1,0,1], goal = 2
Output4
There are 4 contiguous non-empty subarrays whose elements sum to 2.
Example 2
Inputnums = [0,0,0,0,0], goal = 0
Output15
Every non-empty subarray of all zeros has sum 0, giving 15 total subarrays.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • nums[i] is either 0 or 1.
  • 0 <= goal <= nums.length

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate