Compile Ready
Module 5 · Advanced Sliding Window

Binary Subarrays With Sum

MediumProblem 12 of 17 9 min read ~22 min to solve LeetCode
Sliding WindowBinary ArrayCountingPrefix SumAt Most Trick
Asked atGoogleAmazonMicrosoftMetaBloomberg

Problem Statement

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

Input

A binary integer array nums and an integer goal, the exact sum each valid subarray must have.

Output

An integer: the number of contiguous subarrays with sum exactly goal.

Constraints

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

Examples

Example 1

Input:
nums = [1,0,1,0,1], goal = 2
Output: 4
Explanation: The valid subarrays are **[1,0,1]**, **[1,0,1,0]**, **[0,1,0,1]**, and **[1,0,1]** using their positions in the array.

Example 2

Input:
nums = [0,0,0,0,0], goal = 0
Output: 15
Explanation: Every subarray has sum **0**, so the count is **5 * 6 / 2 = 15**.

Example 3

Input:
nums = [1,1,1], goal = 2
Output: 2
Explanation: Only the two length-2 subarrays have sum **2**.

Learning Objectives

  • Recognise exact binary-sum counting as another form of **exactly(K) = atMost(K) - atMost(K - 1)**.
  • Use a variable window because binary values are non-negative and the window sum shrinks monotonically from the left.
  • Handle **goal = 0** correctly by returning **0** for **atMost(-1)**.
  • Explain how zeros create many valid suffixes from one right edge.

Intuition

Pattern Identification

The array is binary, so every element is non-negative. That makes sum <= limit a monotone window condition: expanding right can only increase or preserve the sum, and shrinking left can only decrease or preserve it.

Counting subarrays with sum exactly goal directly is possible with prefix sums, but the advanced sliding-window pattern is cleaner here: count subarrays with sum at most goal, subtract those with sum at most goal - 1, and the remaining subarrays must have sum exactly goal. The guard for negative limits is essential because goal may be 0.

Common mistakes

  • ×Forgetting the negative-limit guard, which breaks cases where **goal = 0**.
  • ×Using this at-most sum trick on arrays with negative numbers, where the monotone window property no longer holds.
  • ×Adding only one valid subarray per right edge instead of **right - left + 1**.
  • ×Shrinking while **sum >= limit** instead of only while **sum > limit**, which removes valid windows whose sum equals the limit.

Algorithm Explanation

Window setup

Build atMost(limit) for binary sums. Track left, windowSum, and total. After adding nums[right], shrink from the left while windowSum > limit. When the window is valid, every suffix ending at right also has sum at most limit, because removing leading binary values cannot increase the sum.

Window visualization

For nums = [1,0,1,0,1] and limit = 2, at right = 3 the window [1,0,1,0] has sum 2. The four suffixes ending there all have sum at most 2, so this step adds 4. At right = 4, adding another 1 makes the sum 3. Shrink past the leftmost 1; the valid window becomes [0,1,0,1], so this step adds 4 more.

Algorithm

  1. Return 0 from atMost(limit) when limit < 0.
  2. Expand right through the array and add nums[right] to windowSum.
  3. While windowSum > limit, subtract nums[left] and move left forward.
  4. Add right - left + 1 to the helper count.
  5. The final answer is atMost(goal) - atMost(goal - 1).

Solutions

Solution: At-most difference on binary sum

Because all values are 0 or 1, the at-most sum condition is monotone and supports a standard variable window. The helper counts all subarrays with sum no larger than a target, and subtraction isolates exactly the requested sum.

Step-by-step

  1. Compute the number of subarrays with sum at most goal.
  2. Compute the number of subarrays with sum at most goal - 1.
  3. In each helper pass, expand right and shrink left only while the sum is too large.
  4. Add the valid suffix count right - left + 1 after every right edge.
  5. Return the difference between the two helper counts.
Time

O(n)

Space

O(1)

Two linear helper passes are still O(n), and the window stores only counters and pointers.

Java implementation

Loading…

Dry Run

Sample input

nums = [1,0,1,0,1], goal = 2. Count atMost(2) and atMost(1), then subtract to isolate sum exactly 2.

phaserightvalueleft after shrinkwindow sumsubarrays addedrunning countmeaning
atMost(2)010111[1]
atMost(2)100123zero keeps both suffixes valid
atMost(2)210236sum reaches the limit
atMost(2)3002410four suffixes are valid
atMost(2)4112414shrink past the first one
atMost(1)010111single one allowed
atMost(1)100123leading zero adds another suffix
atMost(1)211125remove the first one
atMost(1)301138zero extends all valid suffixes
atMost(1)4131210remove zero then one until valid

The helper counts are atMost(2) = 14 and atMost(1) = 10. The difference is 4, exactly the number of subarrays whose binary sum is 2.

Interview Tips

Mention both accepted viewpoints: prefix sums with a hash map and sliding window using atMost. For this course, emphasize why sliding window is legal: the values are binary, so the sum condition is monotone. If the interviewer changes the array to include negative values, switch to prefix sums because the window invariant no longer behaves monotonically.

Likely follow-ups

  • How would you solve the same problem if **nums** could contain negative numbers?
  • How would the code change if the array contained only non-negative values, not just binary values?
  • Can you derive the same answer with prefix sums and a frequency map?
  • Why does **goal = 0** require special care in the at-most helper?

Similar Problems

Key Takeaways

  • Binary arrays make **sum <= limit** a monotone sliding-window invariant.
  • Exact sum can be counted as **atMost(goal) - atMost(goal - 1)**.
  • Zeros matter because they create multiple valid suffixes without increasing the sum.
  • The negative-limit guard makes **goal = 0** work naturally.
Reusable template: For non-negative exact-sum counting, count subarrays with sum at most target and subtract the count with sum at most target minus one.