fork download
  1.  
  2. import java.util.*;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6.  
  7. int n = sc.nextInt();
  8. int k = sc.nextInt();
  9. int arr[] = new int[n];
  10. for (int i = 0; i < n; i++) {
  11. arr[i] = sc.nextInt();
  12. }
  13.  
  14. int p[] = new int[n];
  15. p[0] = arr[0];
  16. for (int i = 1; i < n; i++) {
  17. p[i] = p[i - 1] + arr[i];
  18. }
  19.  
  20. Map<Integer, Integer> hm = new HashMap<>();
  21. hm.put(0, 1);
  22. int count = 0;
  23.  
  24. for (int j = 0; j < n; j++) {
  25. int RHS = p[j] - k * (j + 1);
  26. count += hm.getOrDefault(RHS, 0);
  27. int LHS = RHS;
  28. hm.put(LHS, hm.getOrDefault(LHS, 0) + 1);
  29. }
  30.  
  31. System.out.println(count);
  32. }
  33. }
  34.  
Success #stdin #stdout 0.13s 56620KB
stdin
3
2 2 2
2
stdout
6