Java Two Solutions using sorting and hash Set
Solution one, using sorting and hashset..\nFirst, the array is sorted in ascending order and all the elements of the array are stored in a hashset. Then the sor
Solution one, using sorting and hashset..\nFirst, the array is sorted in ascending order and all the elements of the array are stored in a hashset. Then the sorted array is traversed from the end to the 0 index and whenever we found an element whose negative is also present in the hash set, we return the ans. Else returning -1 in the end.\n```\nclass Solution {\n    public int findMaxK(int[] nums) {\n        Arrays.sort(nums);\n        HashSet<Integer> set = new HashSet<>();\n        for(int i = 0 ; i < nums.length ; i++){\n            set.add(nums[i]);\n        }\n        for(int i = nums.length-1; i>=0 ; i--){//Checking the largest numbers from the last index as the array is sorted\n            if(set.contains(-nums[i])){\n                return nums[i];\n            }\n        }\n        return -1;\n    }\n}\n```\nTime Complexity: O(n Log n) \nNow optimizing the code...\nFirst traversing the entire array and adding all the elements in the hash set. Then initially assuming the ans as -1, iterating through the array and checking for every element whether it is greater than the current ans and its negative is also present in the hash set. Returning this answer variable in the end.\n```\nclass Solution {\n    public int findMaxK(int[] nums) {\n        Set<Integer> set = new HashSet<>();\n        for (int i = 0; i < nums.length; i++){\n            set.add(nums[i]);\n\t\t\t}\n        int ans = -1;\n        for (int i = 0; i < nums.length; i++) {\n            if (nums[i] > ans && set.contains(-nums[i]))\n                ans = nums[i];\n        }\n        return ans;\n    }\n}\n```\nTime Complexity: O(n)
['Sorting', 'Java']