Java Easy Solution Using Two Pointers [ 88.55% ] [ 4ms ]
Approach\n1. Sort the input array nums in non-decreasing order.\n2. Initialize two pointers, st at the beginning of the array and lt at the end of the array.\n3
# Approach\n1. Sort the input array `nums` in non-decreasing order.\n2. Initialize two pointers, `st` at the beginning of the array and `lt` at the end of the array.\n3. Iterate over the array while `st` is less than or equal to `lt`.\n4. In each iteration:\n   - Check if the sum of the elements at `st` and `lt` is greater than zero.\n     - If so, decrement `lt` to move towards smaller elements.\n     - If the sum is less than zero, increment `st` to move towards larger elements.\n5. If the sum is zero, return the element at `lt`, as it represents the maximum value of `k` such that `-k` is also present in the array.\n6. If no such `k` exists, return -1.\n# Complexity\n- Time complexity:\nO(n log n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\n    public int findMaxK(int[] nums) {\n        Arrays.sort(nums);\n        int st=0;\n        int lt=nums.length-1;\n        while(st<=lt){\n            if((nums[st] + nums[lt]) > 0) lt--;\n            else if((nums[st] + nums[lt]) < 0) st++;\n            else return nums[lt];\n        }\n        return -1;\n    }\n}\n```\n\n![193730892e8675ebafc11519f6174bee5c4a5ab0.jpeg](https://assets.leetcode.com/users/images/b0c899de-1c4c-48db-9ba9-37c21107d01a_1718526742.1580913.jpeg)\n
['Java']