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. {
  12. Scanner sc = new Scanner(System.in);
  13. int n = sc.nextInt();
  14. int arr []= new int[n];
  15.  
  16. for(int i=0;i<n;i++){
  17. arr[i] = sc.nextInt();
  18. }
  19. int k = sc.nextInt();
  20. int ans =0;
  21. HashMap<Integer,Integer> map = new HashMap<>();
  22.  
  23. map.put(arr[0],1);
  24.  
  25. for(int i=1;i<n;i++){
  26. int temp = arr[i] + k;
  27. if(map.containsKey(temp)){
  28. ans += map.get(temp);
  29. }
  30. map.put(arr[i],map.getOrDefault(arr[i],0)+1);
  31. }
  32.  
  33. System.out.println(ans);
  34. }
  35. }
Success #stdin #stdout 0.12s 56532KB
stdin
5
1 5 2 4 1
3
stdout
2