fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int count( vector<int> &arr,int k){
  4. int c=0;
  5. unordered_map<int , int> m;
  6. for(int i=0;i<arr.size();i++){
  7. int t=arr[i]+k;
  8. if(m.find(t)!=m.end()){
  9. c+=m[t];
  10. }
  11. m[arr[i]]++;
  12. }
  13. return c;
  14. }
  15. int main() {
  16. vector<int> b = {1, 5, 3, 4, 2};
  17. int k = 2;
  18. cout << "Count of pairs: " << count(b, k) << endl;
  19. return 0;
  20. }
  21.  
  22.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Count of pairs: 2