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. // your code goes here
  13. Scanner sc = new Scanner(System.in);
  14. int N = sc.nextInt();
  15. int Arr[] = new int[N];
  16. for(int i=0; i<N; i++){
  17. Arr[i] = sc.nextInt();
  18. }
  19. int k = sc.nextInt();
  20.  
  21. Map<Integer,Integer> map = new HashMap<>();
  22.  
  23. int sum=0, count=0;
  24. for(int i=0; i<N ; i++){
  25. sum+=Arr[i];
  26. if(sum==k){
  27. count++;
  28. }
  29. count += map.getOrDefault(sum-k,0);
  30. map.put(sum, map.getOrDefault(sum,0)+1);
  31.  
  32. }
  33. System.out.println(count);
  34. }
  35. }
Success #stdin #stdout 0.13s 54564KB
stdin
5
10 2 -2 -20 10
-10
stdout
3