fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int size,k;
  6. cin>>size>>k;
  7. int A[size];
  8. for(int i = 0 ; i<size ; i++){
  9. cin>>A[i];
  10. }
  11. map<int,int>hashmap;
  12. for(int i = 0 ; i<size ; i++){
  13. hashmap[A[i]]++;
  14. }
  15. int totalCount = 0;
  16. if(k%2==0){
  17. int d = k/2;
  18. if(hashmap.find(d)!=hashmap.end()){
  19. totalCount += (hashmap[d]*((hashmap[d])-1))/2;
  20. }
  21. }
  22. for(auto i : hashmap){
  23. if(i.first<(k/2)){
  24. if(hashmap.find((k-i.first))!=hashmap.end()){
  25. int d = i.second;
  26. int e = hashmap[(k-i.first)];
  27. totalCount += (d*e);
  28. }
  29. }
  30. }
  31. cout<<totalCount<<endl;
  32. }
Success #stdin #stdout 0.01s 5284KB
stdin
7 4
3 1 2 2 3 1 1
stdout
7