Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 622 Bytes

binary_subarray_with_sum.md

File metadata and controls

32 lines (25 loc) · 622 Bytes

930. Binary Subarrays With Sum

method 1, fast

concept used

method 2

code
class Solution {
public:
    int numSubarraysWithSum(vector<int>& nums, int goal) {
        int n = nums.size(); 
        map<int, int> mp;
        
        int sum = 0; 
        int ans = 0;
        mp[0] = 1;
        
        for (int i = 0; i < n; i ++) {
            sum += nums[i];
            ans += mp[sum - goal];
            mp[sum] ++;
        }
        return ans;
    }
};