Skip to content

Sliding Window — Binary Subarrays With Sum ​

↗ LeetCode: Binary Subarrays With Sum · Medium · pattern chapter →

Asked atGoogleAmazonMeta

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

Example 1 — nums = [1,0,1,0,1], goal = 2 → 4 (subarrays: [1,0,1], [1,0,1,0], [0,1,0,1], [1,0,1]) Example 2 — nums = [0,0,0,0,0], goal = 0 → 15 (every subarray sums to 0) Example 3 — nums = [1,1,1], goal = 3 → 1

Constraints — 1 ≤ n ≤ 3 · 10⁴; nums[i] ∈ {0, 1}; 0 ≤ goal ≤ n. Brute is O(n²) = 9·10⁸ ops at n=3·10⁴ (TLE past 5 s). Sliding-window atMost(goal) − atMost(goal−1) runs in O(n) = 3·10⁴ ops.

Progressive Hints
Try to solve it yourself first. Reveal hints only when stuck — each hint is progressively more specific.
---
⏱️Interview timer00:00
Practice under interview pressure. Start when you begin reading; stop when you finish. Save best only when you had a full working solution.
Target for interview: Medium = 20-25 min · Hard = 30-40 min · Best time persists on this device only.

Approach 1 — Every subarray ​

Intuition. For each [i, j], sum bits; increment on match.

java
int numSubarraysWithSumBrute(int[] nums, int goal) {
    int n = nums.length, count = 0;
    for (int i = 0; i < n; i++) {
        int s = 0;
        for (int j = i; j < n; j++) {
            s += nums[j];
            if (s == goal) count++;
            else if (s > goal) break;
        }
    }
    return count;
}

Complexity — Time O(n²); Space O(1).


Approach 2 — Prefix-sum hash map (works with any integers) ​

Insight from brute. Precompute prefix sums. Subarray [i, j] has sum goal iff pref[j+1] - pref[i] = goal → count for each j, how many earlier prefixes equal pref[j+1] - goal. Same recipe as Subarray Sum Equals K.

java
int numSubarraysWithSumPS(int[] nums, int goal) {
    Map<Integer, Integer> cnt = new HashMap<>();
    cnt.put(0, 1);
    int pref = 0, res = 0;
    for (int x : nums) {
        pref += x;
        res += cnt.getOrDefault(pref - goal, 0);
        cnt.merge(pref, 1, Integer::sum);
    }
    return res;
}

Complexity — Time O(n); Space O(n).


Approach 3 — atMost(goal) − atMost(goal−1) sliding window ​

Insight from prefix-sum. With non-negative integers, sums are monotone in window size — a plain sliding-window computes atMost(goal) in O(n) and O(1) space. Subtract atMost(goal-1) to get "exactly goal".

java
int numSubarraysWithSum(int[] nums, int goal) {
    return atMost(nums, goal) - atMost(nums, goal - 1);
}
int atMost(int[] nums, int goal) {
    if (goal < 0) return 0;
    int left = 0, sum = 0, res = 0;
    for (int right = 0; right < nums.length; right++) {
        sum += nums[right];
        while (sum > goal) sum -= nums[left++];
        res += right - left + 1;
    }
    return res;
}
Execution Trace
atMost(goal=2) — nums=[1,0,1,0,1]
leftright1001120314sum = 1res = 3
Step 1 / 4subarrays ending at 0 and 1 all valid

Complexity — Time O(n); Space O(1).


Try it yourself ​

Complexity summary ​

ApproachTimeSpaceInterview grade
Every subarrayO(n²)O(1)baseline
Prefix-sum hash mapO(n)O(n)works even for negatives
Two atMost windowsO(n)O(1)optimum for non-negatives

When to use which ​

  • Binary / non-negative array + exact sum → atMost sliding trick.
  • Arbitrary integers (negatives allowed) → prefix-sum hash map — sliding window breaks.
  • Longest / shortest subarray with sum X (not count) → different template — see Minimum Size Subarray Sum.
  • Return subarrays themselves → prefix-sum + track indices, or DP; loses O(n).
✨AI Study CompanionBeta
Was this page helpful?

Released under MIT license.

💡 Quick shortcuts
H HintsR Run JavaA AI Help