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 b[]=new int[n+1];
  16.  
  17. for(int i=1;i<=n;i++)
  18. {
  19. b[i]=sc.nextInt();
  20. }
  21.  
  22. Map<Integer,Integer> m=new HashMap<>();
  23.  
  24. for(int k=3;k<n;k++)
  25. {
  26. for(int l=k+1;l<=n;l++)
  27. {
  28. int sum=b[k]+b[l];
  29. m.put(sum,m.getOrDefault(sum,0)+1);
  30. }
  31. }
  32.  
  33.  
  34. int ans=0;
  35.  
  36. for(int j=2;j<=n-2;j++)
  37. {
  38. for(int i=1;i<j;i++)
  39. {
  40. int sum=(b[i]+b[j]);
  41. if(m.containsKey(-sum))
  42. ans+=m.getOrDefault(-sum,0);
  43. }
  44. for(int j1=j+2;j1<=n;j1++)
  45. {
  46. int pair=b[j+1]+b[j1];
  47. m.put(pair,m.getOrDefault(pair,0)-1);
  48.  
  49. }
  50. }
  51.  
  52. System.out.println(ans);
  53. }
  54. }
Success #stdin #stdout 0.11s 54412KB
stdin
7
1 2 3 4 -1 -2 -2
stdout
3