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 = -1 *target - arr[j];
  18. if(target == 0){
  19. count += mpp[r1];
  20. }
  21. else{
  22. count += mpp[r1];
  23. count += mpp[r2];
  24. }
  25. mpp[arr[j]] = mpp[arr[j]] + 1;
  26. }
  27. cout << count;
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 5332KB
stdin
5
2
10 5 1 3 1
stdout
1