fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  4. int tinh(int a[], int n, int k) {
  5. sort(a, a + n);
  6. int res = 0;
  7. int j=0;
  8. for (int i = 0; i < n; i++) {
  9. while (j < n && a[j] - a[i] < k) {
  10. j++;
  11. }
  12. res += j-i-1;
  13. }
  14. return res;
  15. }
  16.  
  17. int main() {
  18. fastio
  19. int t; cin >> t;
  20. while(t--) {
  21. int n, k; cin >> n >> k; int a[n];
  22. for(int i=0; i<n; i++) cin >> a[i];
  23. cout << tinh(a,n,k) << endl;
  24. }
  25. return 0;
  26. }
Success #stdin #stdout 0s 4252KB
stdin
1
3 7
1 7 8
stdout
2