fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception{
  11. Scanner sc = new Scanner(System.in);
  12. int q=sc.nextInt();
  13. for(int j=0; j<q; j++){
  14. int n=sc.nextInt();
  15. int k=sc.nextInt();
  16. int nums[] = new int[n];
  17. for(int i=0; i<n; i++){
  18. nums[i]=sc.nextInt();
  19. }
  20.  
  21. int res = countDiffOfPairEqualsK(k,nums);
  22. System.out.println(res);
  23. }
  24. }
  25.  
  26. public static int countDiffOfPairEqualsK(int k,int []nums){
  27. HashMap<Integer,Integer> map = new HashMap<>();
  28. int cnt=0;
  29. for(int i=0; i<nums.length; i++){
  30. if(map.containsKey( nums[i] + k)){
  31. cnt+=map.get(nums[i] + k);
  32. }
  33. map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
  34. }
  35.  
  36. return cnt;
  37.  
  38. }
  39. }
Success #stdin #stdout 0.15s 54488KB
stdin
16
5
3
1 5 2 4 1
1
0
7
4
0
3 3 3 3
6
-2
1 3 5 7 9 11
5
10
1 2 3 4 5
4
0
-1 -2 -3 -4
6
5
-3 -1 0 2 4 6
2
1
0 1
5
100
100 200 300 400 500
5
-5
-5 -10 -15 -20 -25
7
0
4 4 4 4 4 4 4
6
3
0 1 2 3 4 5
4
999
1000 2000 3000 4000
5
1
1 1 2 2 3
8
-3
10 7 4 1 -2 -5 -8 -11
6
4
-6 -2 0 2 4 6
stdout
2
0
6
5
0
0
0
0
0
0
21
0
0
0
0
0