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[] b=new int[n];
  15. for(int i=0;i<n;i++){
  16. b[i]=sc.nextInt();
  17. }
  18. int k= sc.nextInt();
  19. int count=0;
  20. HashMap<Integer,Integer> map=new HashMap<>();
  21. for(int i=0;i<n;i++){
  22. int res=k+b[i];
  23. if(map.containsKey(res)){
  24. count+=map.get(res);
  25. }
  26. map.put(b[i],map.getOrDefault(b[i],0)+1);
  27. }
  28. System.out.println(count);
  29. }
  30. }
Success #stdin #stdout 0.17s 54556KB
stdin
5
1 5 2 4 1
3
stdout
2