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.  
  17. int tot=0;
  18. for(int i=0;i<n;i++)
  19. {
  20. a[i]=sc.nextInt();
  21. tot+=a[i];
  22. }
  23.  
  24. int lhs=0,rhs=0;
  25. int prefix=0;
  26.  
  27. int y=0;
  28. int ans=0;
  29. Map<Integer,Integer> m=new HashMap<>();
  30. //we are going till n-1 because lhs part we are checking
  31. for(int i=0;i<n-1;i++)
  32. {
  33. y=tot/3;
  34. //sum of ele from 0.....i
  35. prefix+=a[i];
  36.  
  37. //putting stick after every element for partition
  38. lhs+=a[i];
  39.  
  40. rhs=tot-lhs;
  41.  
  42. if(lhs==2*y && rhs==y)
  43. {
  44. ans+=m.get(y);
  45. }
  46. m.put(prefix,m.getOrDefault(prefix,0)+1);
  47.  
  48.  
  49. }
  50.  
  51. System.out.println(ans);
  52. }
  53. }
Success #stdin #stdout 0.12s 56380KB
stdin
5
1 2 3 0 3
stdout
2