Advertisement
Saleh127

two pointer

Sep 27th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. int main()
  6. {
  7.  
  8. test
  9. {
  10. ll a[100005],c,d,e=0,f,i,j,k,l;
  11. cin>>c>>d;
  12. for(i=0;i<c;i++)
  13. {
  14. cin>>a[i];
  15. }
  16. sort(a,a+c);
  17. i=0;
  18. j=c-1;
  19.  
  20. while(i<j && i<c && j>=0)
  21. {
  22. if((a[i]+a[j])==d)
  23. {
  24. e++;
  25. i++;
  26. j--;
  27. }
  28. else if((a[i]+a[j])<d)
  29. {
  30. i+=1;
  31. }
  32. else if((a[i]+a[j])>d)
  33. {
  34. j-=1;
  35. }
  36. }
  37. cout<<"Case #"<<cs<<": "<<e<<endl;
  38. }
  39. return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement