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