fork(2) download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. int n,k,cnt=0,i;
  9. scanf("%d%d",&n,&k);
  10. int arr[n];
  11. for(i=0;i<n;i++)
  12. {
  13. scanf("%d",&arr[i]);
  14. }
  15. sort(arr,arr+n);
  16. for(i=0;i<n;i++)
  17. {
  18. if(binary_search(arr+i+1,arr+n,arr[i]+k))
  19. {
  20. cnt++;
  21. }
  22.  
  23. }
  24. printf("%d",cnt);
  25. return 0;
  26. }
Success #stdin #stdout 0s 3144KB
stdin
5 2
1 5 3 4 2
stdout
3