fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. main(){
  5. int n,k;
  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. int cnt=0;
  14. for(auto &it:mp){
  15. if(k==0) cnt+=(it.second*(it.second-1))/2;
  16. else{
  17. if(mp.find(it.first-k)!=mp.end()){
  18. cnt+=mp[it.first-k]*it.second;
  19. }
  20. if(mp.find(it.first+k)!=mp.end()){
  21. cnt+=mp[it.first+k]*it.second;
  22. }
  23. mp.erase(it.first);
  24. }
  25. }
  26. cout<<cnt<<"\n";
  27. }
Success #stdin #stdout 0.01s 5280KB
stdin
6 4
0 4 8 12 16 16
stdout
2