fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int getCount(vector<int>& arr,int k){
  4. int n=arr.size();
  5. int count=0;
  6. for(int i=0,j=n-1;i<n;i++){
  7. int sum=arr[i]+arr[j];
  8. while(sum>k && i!=j){
  9. j--;
  10. sum=arr[i]+arr[j];
  11. }
  12. if(i==j){
  13. break;
  14. }
  15. count=count+(j-i);
  16. }
  17. return count;
  18. }
  19.  
  20. int main() {
  21. // your code goes here
  22. int n;
  23. cin>>n;
  24. vector<int>arr(n);
  25. for(int i=0;i<n;i++){
  26. cin>>arr[i];
  27. }
  28. sort(arr.begin(),arr.end());
  29. int L;
  30. cin>>L;
  31. int R;
  32. cin>>R;
  33. int result1=getCount(arr,L-1);
  34. int result2=getCount(arr,R);
  35. cout<<"The sum of pair in the given range is:"<<result2-result1;
  36. return 0;
  37. }
Success #stdin #stdout 0s 5320KB
stdin
7
0 1 2 3 4 5 6 
3
5
stdout
The sum of pair in the given range is:7