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. int[] arr = {-1,2,-3,3};
  13. System.out.println(findMax(arr));
  14. }
  15.  
  16. static int findMax(int[] nums){
  17. int max = Integer.MIN_VALUE;
  18.  
  19.  
  20. int n = nums.length;
  21.  
  22. Arrays.sort(nums);
  23.  
  24. int left = 0;
  25. int right = n - 1;
  26.  
  27. while(left < right){
  28. int sum = nums[right] + nums[left];
  29.  
  30. if(sum == 0){
  31. return nums[right];
  32. }
  33.  
  34. else if(sum < 0){
  35. left++;
  36. }
  37. else{
  38. right--;
  39. }
  40.  
  41. }
  42.  
  43. return -1;
  44.  
  45. }
  46. }
Success #stdin #stdout 0.09s 54748KB
stdin
Standard input is empty
stdout
3