-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_subarray_sum.cpp
More file actions
48 lines (35 loc) · 1.17 KB
/
5_subarray_sum.cpp
File metadata and controls
48 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// map to store frequency of prefix sums
unordered_map<int, int> mp;
// base case: prefix sum 0 occurs once
// this handles subarrays starting from index 0
mp[0] = 1;
int sum = 0; // running prefix sum
int cnt = 0; // number of subarrays with sum = k
for (int x : arr) {
sum += x; // add current element to prefix sum
// if (sum - k) existed before,
// it means there is a subarray ending here with sum = k
if (mp.find(sum - k) != mp.end()) {
cnt += mp[sum - k];
}
// store current prefix sum frequency
mp[sum]++;
}
// final answer
cout << cnt << "\n";
}
// sum : keeps track of the prefix sum up to current index
// mp : stores frequency of each prefix sum seen so far
// mp[s] means: how many times prefix sum = s has occurred
// if (sum - k) exists in map,
// it means there are subarrays ending at current index with sum = k
// cnt : total number of subarrays whose sum is equal to k