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. ll count =0;
  19.  
  20. unordered_map<ll, ll> mpp;
  21.  
  22. for(int j=0;j<n ;j++){
  23. ll r1= target - A[j];
  24. ll r2= -1*target - A[j];
  25.  
  26. if(target==0)
  27. count+=mpp[r1];
  28.  
  29. else{
  30. count+= mpp[r1];
  31. count+= mpp[r2];
  32. }
  33.  
  34. mpp[A[j]]++;
  35.  
  36. }
  37. cout<<count<<endl;
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 5320KB
stdin
5
10 5 1 3 1
2 
stdout
1