fork download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6.  
  7. int sum = sc.nextInt();
  8. int n = sc.nextInt();
  9. int arr[] = new int[n];
  10.  
  11. for (int i = 0; i < n; i++) {
  12. arr[i] = sc.nextInt();
  13. }
  14.  
  15. HashMap<Integer, Integer> hm = new HashMap<>();
  16. int count = 0;
  17.  
  18. for (int i = 0; i < n; i++) {
  19. int a = arr[i];
  20.  
  21.  
  22. int targetValue = a + sum;
  23. if (hm.containsKey(targetValue)) {
  24. count += hm.get(targetValue);
  25. }
  26.  
  27.  
  28. int targetValue2 = a - sum;
  29. if (hm.containsKey(targetValue2)) {
  30. count += hm.get(targetValue2);
  31. }
  32.  
  33. hm.put(a, hm.getOrDefault(a, 0) + 1);
  34. }
  35.  
  36. System.out.println(count);
  37. }
  38. }
Success #stdin #stdout 0.18s 56648KB
stdin
4
2
2
2
stdout
0