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 a[]=new int[n];
  16. for(int i=0;i<n;i++)
  17. a[i]=sc.nextInt();
  18. int k=sc.nextInt();
  19.  
  20. Map<Integer,Integer> hash=new HashMap<>();
  21. int sum=0,c=0;
  22. hash.put(0,1);
  23.  
  24. for(int ele:a)
  25. {
  26. sum+=ele;
  27. int cpl=sum-k;
  28. if(hash.containsKey(cpl))
  29. c+=hash.get(cpl);
  30. hash.put(sum,hash.getOrDefault(sum,0)+1);
  31.  
  32. }
  33.  
  34. System.out.println(c);
  35. }
  36. }
Success #stdin #stdout 0.13s 56532KB
stdin
5
1 2 3 4 5 
5
stdout
2