 All Problems
Binary Subarrays With Sum
medium
sliding window
prefix sum
hash map
amazon
google

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

Example 1:

Input: 1 0 1 0 1
goal: 2
Output: 4

Example 2:

Input: 0 0 0 0 0
goal: 0
Output: 15

Constraints:

  • 1 ≤ nums.length ≤ 3 × 10⁴
  • nums[i] is 0 or 1
  • 0 ≤ goal ≤ nums.length

Input format: First line: space-separated binary array. Second line: goal.

Output format: Count of subarrays with sum == goal.

Run to check your code against the sample cases, or submit to run every case