fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int count = 0;
  6. int n,x;
  7. cin >> n >> x;
  8. int A[n];
  9. for (int i = 0; i < n; i++){
  10. cin >> A[i];
  11. }
  12. sort(A, A + n);
  13. int f = 0;
  14. int e = n -1;
  15. //for (int i = 0; i < n; i++){
  16. // cout << A[i];
  17. //}
  18. while (e >= f){
  19. if (A[f] + A[e] == x){
  20. count++;
  21. f++;
  22. }
  23. else if (A[f] + A[e] > x){
  24. e--;
  25. }
  26. else{
  27. f++;
  28. }
  29. }
  30. cout << count;
  31. }
  32.  
Success #stdin #stdout 0.01s 5288KB
stdin
7 6
1 2 4 3 4 5 3 
stdout
4