fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int t;
  6. cin >> t;
  7. while(t--)
  8. {
  9. int n,k,c=0;
  10. cin >> n >> k;
  11. vector<int> v;
  12. for(int i=0;i<n;i++)
  13. {
  14. int a;
  15. cin >> a;
  16. v.push_back(a);
  17. }
  18. sort(v.begin(),v.end());
  19. int i=0,j=n-1;
  20. while(i<j)
  21. {
  22. if(v[i]+v[j]==k)
  23. {
  24. c++;
  25. i++;
  26. j--;
  27. }
  28. else if(v[i]+v[j]<k)
  29. {
  30. i++;
  31. }
  32. else
  33. {
  34. j--;
  35. }
  36. }
  37. cout << c << endl;
  38. }
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5316KB
stdin
4
4 4
1 2 3 2
8 15
1 2 3 4 5 6 7 8
6 1
1 1 1 1 1 1
16 9
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3
stdout
2
1
0
4