fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n;
  7. cin>>n;
  8. vector<int>arr(n);
  9. for(int i=0;i<n;i++){
  10. cin>>arr[i];
  11. }
  12. sort(arr.begin(),arr.end());
  13. int k;
  14. cin>>k;
  15. int count=0;
  16. for(int i=0,j=n-1;i<n;i++){
  17. int sum=arr[i]+arr[j];
  18. while(sum>k && i!=j){
  19. j--;
  20. sum=arr[i]+arr[j];
  21. }
  22. if(i==j){
  23. break;
  24. }
  25. count=count+(j-i);
  26. }
  27.  
  28. cout<<count;
  29. return 0;
  30. }
Success #stdin #stdout 0s 5268KB
stdin
6
1 2 3 4 5 6
5
stdout
4