fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, k;
  6. cin>>n>>k;
  7. vector<int> arr(n);
  8. for(int i=0;i<n;i++)
  9. cin>>arr[i];
  10. unordered_map<int, int> mp;
  11. mp[0]=-1;
  12. int currSum=0;
  13. int count=0;
  14. int minLen=INT_MAX;
  15. for(int j=0;j<n;j++){
  16. currSum+=arr[j];
  17. int toFind=currSum-k;
  18. if(mp.find(toFind)!=mp.end()){
  19. int idx=mp[toFind];
  20. int length=j-idx;
  21. if(length<minLen){
  22. minLen=length;
  23. //we found a new shorter length
  24. //reset count
  25. count=1;
  26. }
  27. else if(length==minLen)
  28. count++;
  29.  
  30. }
  31. mp[currSum]=j;
  32. }
  33. cout<<count;
  34. return 0;
  35. }
Success #stdin #stdout 0s 544KB
stdin
6 5
3 2 1 4 2 3
stdout
3