fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int counting(int arr[],int n,int k){
  4. unordered_map<int,int>a;
  5. int sum=0;
  6. int count=0;
  7. for(int i=0;i<n;i++){
  8. sum+=arr[i];
  9. if(sum==k){
  10. count++;
  11. }
  12. int x=sum-k;
  13. if(a.find(x)!=a.end()){
  14. count+=a[x];
  15. }
  16. a[arr[i]]=i;
  17. }
  18.  
  19. return count;
  20. }
  21.  
  22. int main() {
  23. // your code goes here
  24. int arr[]={1,2,3,6,5};
  25. int k=5;
  26. int n=sizeof(arr)/sizeof(arr[0]);
  27. int ans=counting(arr,n,k);
  28. cout<<ans;
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty