fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int n,k,cnt=0;
  6. cin>>n>>k;
  7. vector<int>v(n);
  8. unordered_map<int,int>mp;
  9. for(auto &it:v){
  10. cin>>it;
  11. mp[it]++;
  12. }
  13. for(auto &it:mp){
  14. if(mp.find(it.first-k)!=mp.end()){
  15. if(k==0) cnt+=(it.second*(it.second-1))/2;
  16. else cnt+=it.second*mp[it.first-k];
  17.  
  18. }
  19. }
  20. cout<<cnt<<"\n";
  21. return 0;
  22. }
Success #stdin #stdout 0s 5300KB
stdin
5 2
1 5 3 4 2
stdout
3