# Binary Subarrays with Sum(Leetcode 930)

Problem Link: <https://leetcode.com/problems/binary-subarrays-with-sum/>

```python
class Solution:
    def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
        
        
        def atmostSum(nums,k):
            
            j=0
            ans=0
            c=0
            n=len(nums)
            for i in range(n):
                if(nums[i]==1):
                    c+=1
                
                while(j<=i and c>k):
                    if(nums[j]==1):
                        c-=1
                        j+=1
                    else:
                        j+=1
                ans=ans+(i-j+1)
            
            return ans
        
        return atmostSum(nums,goal)-atmostSum(nums,goal-1)
```
