fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int t;
  9. cin >> t;
  10. while (t--) {
  11. int n, m;
  12. cin >> n >> m;
  13. vector<int> a(n);
  14. for (int i = 0; i < n; i++) cin >> a[i];
  15.  
  16. sort(a.begin(), a.end());
  17.  
  18. int left = 0, right = n - 1;
  19. int countPairs = 0;
  20.  
  21. while (left < right) {
  22. int sum = a[left] + a[right];
  23. if (sum == m) {
  24. countPairs++;
  25. left++;
  26. right--;
  27. } else if (sum < m) {
  28. left++;
  29. } else {
  30. right--;
  31. }
  32. }
  33.  
  34. cout << countPairs << "\n";
  35. }
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty