fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n;
  7. cin>>n;
  8.  
  9. vector<int> values(n);
  10. for(int i = 0; i < n; i++){
  11. cin>>values[i];
  12. }
  13. int k;
  14. cin>> k;
  15.  
  16. int sum = 0, cnt = 0;
  17. unordered_map<int,int> ump;
  18.  
  19. //edge case
  20. ump[0] = 1;
  21.  
  22. for(int i = 0; i < n; i++){
  23.  
  24. sum += values[i];
  25.  
  26. // Find the remainder
  27. int rem = sum - k;
  28.  
  29. // Find the frequency of it in the left
  30. if(ump.count(rem)){
  31. cnt += ump[rem];
  32. }
  33.  
  34. ump[sum]++;
  35. }
  36.  
  37. cout<<cnt<<endl;
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 5324KB
stdin
10
2 2 3 5 2 2 3 2 2 1
8
stdout
2