3
int subarraySum(vector<int> &arr, int k) {4
int n = arr.size(); // take the size of the array6
int prefix[n]; // make a prefix array to store prefix sum8
prefix[0] = arr[0]; // for element at index at zero, it is same10
// making our prefix array11
for (int i = 1; i < n; i++) {12
prefix[i] = arr[i] + prefix[i - 1];15
unordered_map<int, int> mp; // declare an unordered map17
int ans = 0; // to store the number of our subarrays having sum as 'k'19
for (int i = 0; i < n; i++) // traverse from the prefix array21
if (prefix[i] == k) // if it already becomes equal to k, then increment ans24
// now, as we discussed find whether (prefix[i] - k) present in map or not25
if (mp.find(prefix[i] - k) != mp.end()) {26
ans += mp[prefix[i] - k]; // if yes, then add it our answer29
mp[prefix[i]]++; // put prefix sum into our map32
return ans; // and at last, return our answer