Binary Subarray with Sum

Counting Subarrays and Fixed Window DSA practice problem on Onlearn.

Difficulty: easy.

Topics: Binary subarray with sum, Arrays, Subarray, Prefix Sum, Hash Map, Two Pointers, Sliding Window, Time Complexity, Space Complexity, Loops, Optimization, sliding window, prefix sum, subarray algorithms, array, hash map, Bitwise Operations, Subarray/Substring Problems.

Problem Statement Given a binary array nums (containing only 0s and 1s) and an integer goal, return the number of non empty subarrays that have a sum equal to goal. A subarray is a contiguous part of an array. Input Specification The input will consist of: An array nums of integers, where nums[i] is either 0 or 1. An integer goal. Output Specification Return a single integer representing the total count of subarrays whose sum is equal to goal. Constraints 1 <= nums.length <= 3 10^4 nums[i] is 0 or 1. 0 <= goal <= nums.length Sample Test Cases Sample 1: Explanation: The subarrays with sum equal to 2 are [1,0,1] (indices 0 2) and [1,0,1] (indices 2 4). Sample 2: Explanation: Every non empty subarray has a sum of 0. For an array of length N, there are N (N+1)/2 non empty subarrays. For N=5, 5 (5+1)/2 = 15. Sample 3: Explanation: The subarrays with sum equal to 2 are [1,1] (indices 0 1), [1,1] (indices 1 2), [1,1] (indices 2 3), and [1,1] (indices 3 4).