fork download
  1. #include <iostream>
  2. #include<unordered_set>
  3. using namespace std;
  4.  
  5. int main() {
  6. int n,k;
  7. cin>>n>>k;
  8. unordered_set<int>temp;
  9. int arr[n];
  10. for(int i=0;i<n;i++)
  11. {
  12. cin>>arr[i];
  13. temp.insert(arr[i]);
  14. }
  15. int r=0;
  16. for(int i=0;i<n;i++)
  17. {
  18. int k1=k;
  19. while(k1>0)
  20. {
  21. if(temp.find(arr[i]-k1)!=temp.end())
  22. {
  23. r=r+1;
  24. //cout<<temp.find(arr[i]-1)<<"\n";
  25. }
  26. if(temp.find(arr[i]+k1)!=temp.end())
  27. {
  28. r=r+1;
  29. //cout<<temp.find(arr[i]-2)<<"\n";
  30. }
  31. k1--;
  32. }
  33. }
  34. int result=(r/2)+n;
  35.  
  36. cout<<result;
  37. return 0;
  38. }
Success #stdin #stdout 0s 15240KB
stdin
1 3 
2
stdout
1