fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5.  
  6. int main() {
  7. int n;
  8. cin >> n;
  9.  
  10. vector<ll> A(n);
  11. for (int i = 0; i < n; i++) {
  12. cin >> A[i];
  13. }
  14.  
  15. ll target;
  16. cin >> target;
  17.  
  18.  
  19. ll count =0;
  20.  
  21. unordered_map<ll, ll> mpp;
  22.  
  23. for(int j=0;j<n ;j++){
  24. int r1= A[j]+target;
  25. int r2= A[j]- target;
  26.  
  27. if(target==0)
  28. count+=mpp[r1];
  29.  
  30. else{
  31. count+= mpp[r1];
  32. count+= mpp[r2];
  33. }
  34.  
  35. mpp[A[j]]++;
  36.  
  37. }
  38. cout<<count<<endl;
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 5312KB
stdin
5
10 5 7 3 8
2
stdout
3