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