fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n ;
  6. cin>>n;
  7. int target;
  8. cin>>target;
  9. int arr[n];
  10. for(int i = 0; i< n ; i++){
  11. cin>>arr[i];
  12. }
  13. int count = 0;
  14. unordered_map<int,int> mpp;
  15. for(int j = 0; j< n ; j++){
  16. int r1 = target - arr[j];
  17. int r2 = target + arr[j];
  18. if(mpp.count(r1)){
  19. count += mpp[r1];
  20. }
  21. if(mpp.count(r2)){
  22. count += mpp[r2];
  23. }
  24. mpp[arr[j]] = mpp[arr[j]] + 1;
  25. }
  26. cout << count;
  27.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0.01s 5308KB
stdin
5
10 5 7 3 8
2
stdout
2