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.  
  18. int max = Integer.MIN_VALUE;
  19.  
  20.  
  21. int n = nums.length;
  22. HashSet<Integer> set = new HashSet<>();
  23.  
  24. for(int i=0;i<n;i++){
  25. set.add(nums[i]);
  26. }
  27.  
  28. for(int i=0;i<n;i++){
  29. if(set.contains(-nums[i])){
  30. max = Math.max(max,Math.abs(nums[i]));
  31. }
  32. }
  33. if(max == Integer.MIN_VALUE){
  34. return -1;
  35. }
  36.  
  37. return max;
  38.  
  39. }
  40. }
Success #stdin #stdout 0.08s 54624KB
stdin
Standard input is empty
stdout
3