fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int ans(vector<int> &v,int &k){
  5. unordered_map<int,int>m;
  6. int count=0;
  7. for(int i=0;i<v.size();i++){
  8. int c= k+v[i];
  9. if(m[c]>0)count+=m[c];
  10.  
  11. m[v[i]]++;
  12. }
  13. return count;
  14. }
  15. int main() {
  16. // your code goes here
  17. int n;cin>>n;
  18. vector<int>v(n);
  19. for(int i=0;i<n;i++)cin>>v[i];
  20. int k;cin>>k;
  21. cout<<ans(v,k)<<endl;
  22. return 0;
  23. }
Success #stdin #stdout 0.01s 5320KB
stdin
5
1 5 2 4 1
3
stdout
2