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. hashmap[A[i]]++;
  18. }
  19. cout<<totalCount<<endl;
  20. return 0;
  21. }
Success #stdin #stdout 0s 5308KB
stdin
7 2
3 1 3 1 4 2 3
stdout
4