Binary Subarrays With Sum

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

Given a binary array `nums` and an integer `goal`, return the number of non-empty contiguous subarrays whose elements sum to `goal`.

- `1 <= nums.length <= 3 * 10^4` - `nums[i]` is either `0` or `1` - `0 <= goal <= nums.length` - The interactive visualizer accepts at most 12 values; longer valid arrays are execution-only.

Example 1: input nums = [1,0,1,0,1], goal = 2; output 4. The matching ranges are [0..2], [0..3], [1..4], and [2..4].

Example 2: input nums = [0,0,0,0], goal = 0; output 10. Every non-empty contiguous range sums to zero, giving 4 * 5 / 2 = 10 ranges.

Category: Sliding Window. Topic: sliding-window. Difficulty: Medium. Patterns: sliding-window, prefix-sum.