fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. int main() {
  7. // your code goes here
  8. int n;
  9. cin>>n;
  10. vector<int>arr(n);
  11. for(int i=0;i<n;i++){
  12. cin>>arr[i];
  13. }
  14. int k;
  15. cin>>k;
  16. unordered_map<int,int>mpp;//el,respective count
  17. int ans=0;
  18. for(int el:arr){
  19. if(mpp.find(el+k)!=mpp.end())ans+=mpp[el+k];
  20. if(mpp.find(el-k)!=mpp.end())ans+=mpp[el-k];
  21. mpp[el]++;
  22. }
  23. cout<<ans;
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 5316KB
stdin
4
1 2 2 1
1
stdout
4