fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, k;
  6. cin >> n >> k;
  7.  
  8. vector<int> a(n);
  9.  
  10. for(int i = 0; i < n; i++) {
  11. cin >> a[i];
  12. }
  13.  
  14.  
  15.  
  16. unordered_map<int,int>mp;
  17. int cnt = 0;
  18.  
  19. for(int i = 0 ; i < n ;i++){
  20.  
  21. int cur = a[i] - k;
  22. int cur1 = k + a[i];
  23.  
  24. if(mp.find(cur) != mp.end()){
  25. cnt += mp[cur];
  26. }
  27.  
  28. if(k != 0 && mp.find(cur1) != mp.end()){
  29. cnt += mp[cur1];
  30. }
  31. mp[a[i]]++;
  32. }
  33. cout<<cnt;
  34. return 0;
  35. }
Success #stdin #stdout 0s 5316KB
stdin
5 2
1 5 3 4 2
stdout
3