TWO POINTERS + SORT || Solution of largest positive integer that exists with its negative problem
This was a daily challenge for May 2th 2024.\n\n# Approach\n- Given an integer array nums that does not contain any zeros, find the largest positive integer k s
# This was a daily challenge for May 2th 2024.\n\n# Approach\n- Given an integer array nums that does not contain any zeros, find the largest positive integer `k` such that `-k` also exists in the array. Return the positive integer `k`. If there is no such integer, return `-1`.\n- We use two pointer technique for this problem. Two pointers is really an easy and effective technique that is typically used for searching pairs in a sorted array. The algorithm basically uses the fact that the input arrays are sorted.\n- We start from the zero and last index of the array and conditionally move both pointers. We move the right pointer when the value of the left pointer is less than the value of the right one in absolute value. The same logic applies to the left pointer. If both values \u200B\u200Bare equal to each other in absolute value, we return the answer. Otherwise we return -1\n\n# Complexity\n- Time complexity: $$O(nlogn)$$ - as we use `sort()` function\n- Space complexity: $$O(1)$$ - as no extra space is required\n\n# Code\n```\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        left, right = 0, len(nums) - 1\n        nums.sort()\n        while left < right and nums[left] < 0:\n            if abs(nums[left]) == nums[right]:\n                return nums[right]\n            else:\n                if abs(nums[left]) > nums[right]:\n                    left += 1\n                else:\n                    right -= 1\n        return -1\n```
['Array', 'Hash Table', 'Two Pointers', 'Sorting', 'Python3']