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.  
  11. static int find(int b[], int k )
  12. {
  13. int n=b.length;
  14. Map<Integer,Integer> m=new HashMap<>();
  15. Map<Integer,Integer> used=new HashMap<>();
  16.  
  17. for(int ele:b)
  18. m.put(ele,m.getOrDefault(ele,0)+1);
  19.  
  20. int ans=0;
  21. for(int ele:b)
  22. {
  23. int cpl=k-ele;
  24. if(m.containsKey(cpl))
  25. {
  26. if(!used.containsKey(ele) && !used.containsKey(cpl))
  27. {
  28. if(cpl==ele)
  29. ans+=m.get(cpl)/2;
  30. else
  31. ans+=Math.min(m.get(ele), m.get(cpl));
  32.  
  33. used.put(ele,1);
  34. used.put(cpl,1);
  35. }
  36. }
  37. }
  38. return ans;
  39. }
  40. public static void main (String[] args) throws java.lang.Exception
  41. {
  42. // your code goes here
  43. Scanner sc=new Scanner(System.in);
  44. int n=sc.nextInt();
  45. int b[]=new int[n];
  46. for(int i=0;i<n;i++)
  47. b[i]=sc.nextInt();
  48.  
  49. int temp=0;
  50. for(int i=0;i<n;i++)
  51. {
  52. for(int j=i+1;j<n;j++)
  53. {
  54. int k=b[i]+b[j];
  55. temp=Math.max(temp,find(b,k));
  56. }
  57. }
  58. System.out.println(temp);
  59. }
  60. }
Success #stdin #stdout 0.11s 54432KB
stdin
8
1 2 1 2 5 5 1 2 
stdout
3