<p>Given a binary array <code>nums</code> and an integer <code>goal</code>, return <em>the number of non-empty <strong>subarrays</strong> with a sum</em> <code>goal</code>.</p>

<p>A <strong>subarray</strong> is a contiguous part of the array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,0,1,0,1], goal = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 4 subarrays are bolded and underlined below:
[<u><strong>1,0,1</strong></u>,0,1]
[<u><strong>1,0,1,0</strong></u>,1]
[1,<u><strong>0,1,0,1</strong></u>]
[1,0,<u><strong>1,0,1</strong></u>]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,0,0,0,0], goal = 0
<strong>Output:</strong> 15
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
	<li><code>0 &lt;= goal &lt;= nums.length</code></li>
</ul>

class Solution {
public:
    int sumLessthanEqualTo_k(vector<int> arr,int k,int n){
        int l=0,r=0,sum=0,cnt=0;
        while(r<n){
            sum+=arr[r];
            while(sum>k && l<=r){
                sum-=arr[l];
                l++;
            }
            cnt+=r-l+1;
            r++;
        }
        return cnt;
    }
    int numSubarraysWithSum(vector<int>& arr, int k) {
        int n = arr.size();
        cout<<sumLessthanEqualTo_k(arr,k,n);
        int ans = sumLessthanEqualTo_k(arr,k,n) - sumLessthanEqualTo_k(arr,k-1,n);
        return ans;
        




        // while(r<n){
        //     sum+=arr[r];
        //     if(sum==k){
        //          cnt++;
        //          int temp = l;
        //         while (temp < r && sum == k) {
        //             cnt++;
        //             sum -= arr[temp++];
        //         }
        //     }
        //     r++;
        // }
        // return cnt;
    }
};
34700