fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int size,k;
  6. cin>>size>>k;
  7. int A[size];
  8. for(int i = 0 ; i<size ; i++){
  9. cin>>A[i];
  10. }
  11. map<int,int>hashmap;
  12. int totalCount = 0;
  13. for(int i = size-1 ; i>=0 ; i--){
  14. if(hashmap.find(A[i]-k)!=hashmap.end()){
  15. totalCount += hashmap[(A[i]-k)];
  16. }
  17. if(hashmap.find(A[i]+k)!=hashmap.end()){
  18. totalCount += hashmap[(A[i]+k)];
  19. }
  20. hashmap[A[i]]++;
  21. }
  22. cout<<totalCount<<endl;
  23. return 0;
  24. }
Success #stdin #stdout 0s 5324KB
stdin
5 2
1 5 3 4 2
stdout
3