fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. main(){
  5. int n,k,cnt=0;
  6. cin>>n>>k;
  7. vector<int>v(n);
  8. for(auto &it:v)cin>>it;
  9. sort(v.begin(),v.end());
  10. for(int i=0,j=1;j<n;j++){
  11. int d= v[j]-v[i];
  12. while(d>k){
  13. i++;
  14. d=v[j]-v[i];
  15. }
  16. cnt+=j-i;
  17. }
  18. cout<<cnt<<"\n";
  19. }
Success #stdin #stdout 0s 5312KB
stdin
5 5
4 3 7 1 2
stdout
9