MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / main

Function main

CSES_Problems/Subarray Sum - II/solution.cpp:18–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16*/
17
18signed main() {
19 int n, x;
20 cin >> n >> x;
21 int a[n];
22 for(int i = 0; i < n; i++)
23 cin >> a[i];
24 map<int, int> m; // map will keep track for the sum from the first index to
25 m[0] = 1;
26 int ans = 0;
27 int curr = 0;
28 for(int i = 0; i < n; i++) {
29 curr += a[i];
30 int req = curr - x;
31 if(m.count(req)) {
32 ans += m[req];
33 }
34 m[curr]++;
35 }
36 cout << ans <<"\n";
37 return 0;
38}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected