fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. #define fo(i,start,end) for(int i=start;i<end;i++)
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. int arr[] = { 1,0,1,2,10,8};
  9. int n = 6;
  10. int k = 3;
  11. int count = 0;
  12. unordered_map<int,int>mp;
  13. int prefix[n] = {0};
  14. int sum = 0;
  15. mp[0] = 1; //i have seen sum of zero 1 time
  16. fo(i,0,n){
  17. sum += arr[i];
  18. mp[sum]++;
  19. int complement = sum-k;
  20. count += mp[complement];
  21. // prefix[i] = sum;
  22.  
  23. }
  24.  
  25. cout<<count;
  26. return 0;
  27. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
2