fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <set>
  4. using namespace std;
  5.  
  6. int contar_pares(int diff, set<int> numeros){
  7. int cont = 0;
  8. for (auto num: numeros){
  9. if (numeros.find(num - diff) != numeros.end()){
  10. cont++;
  11. }
  12. if (numeros.find(num + diff) != numeros.end()){
  13. cont++;
  14. }
  15. }
  16. return cont / 2;
  17. }
  18.  
  19. int main(){
  20. int n, k;
  21. cin >> n >> k;
  22. int tamanho = n;
  23. set<int> numeros;
  24. for(int i = 0; i < tamanho; i++){
  25. int num;
  26. cin >> num;
  27. numeros.insert(num);
  28. }
  29. cout << contar_pares(k, numeros) << endl;
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 15240KB
stdin
5 2  

1 5 3 4 2
stdout
3