fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n,target;
  7. cin>>n>>target;
  8. int arr[n];
  9. for(int i = 0; i < n; i++){
  10. cin>>arr[i];
  11. }
  12. unordered_map<int,int> ump;
  13.  
  14. int cnt = 0;
  15.  
  16. for(int j = 0; j < n; j++){
  17. int r = target - arr[j];
  18.  
  19. //Here ump[r] tells us the frequency of targer-arr[j] in the left side of j
  20.  
  21. if(ump.count(r)){
  22. cnt += ump[r];
  23. }
  24.  
  25. ump[arr[j]]++;
  26. }
  27. cout<<cnt<<endl;
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5304KB
stdin
6
5
2
2
3
3
5
5
stdout
4