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