fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. int n = scanner.nextInt();
  7. long k = scanner.nextLong();
  8. long[] b = new long[n];
  9.  
  10. for (int i = 0; i < n; i++) {
  11. b[i] = scanner.nextLong();
  12. }
  13.  
  14. long count = 0;
  15.  
  16. Arrays.sort(b);
  17.  
  18. for (int i = 0, j = 0; j < n; j++) {
  19. long d = b[j] - b[i]; //[i............j]
  20. while (d > k) {
  21. i++;
  22. d = b[j] - b[i];
  23. }
  24. count += (j - i + 1);
  25. }
  26.  
  27. System.out.println(count - n);
  28. scanner.close();
  29. }
  30. }
Success #stdin #stdout 0.11s 56572KB
stdin
3 4
1 3 4
stdout
3