fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int countPairs(vector<int>&b,int k){
  4. unordered_map<int,int>f;
  5. int cnt=0;
  6. for(int i=0;i<b.size();i++){
  7. int req=b[i]-k;
  8. if(f.find(req)!=f.end()){
  9. cnt+=f[req];
  10. }
  11. f[b[i]]++;
  12. }
  13. return cnt;
  14. }
  15. int main(){
  16. vector<int>b={5,3,6,2,5};int k=2;
  17. cout<<countPairs(b,k)<<"\n";
  18. return 0;
  19. }
  20.  
Success #stdin #stdout 0.01s 5264KB
stdin
Standard input is empty
stdout
1