Python3 || O(n) TIME AND SPACE || May 2 2024 Daily
Intuition\nThe problem requires finding the largest positive integer k such that -k also exists in the given array nums. To solve this, we can use a set to effi
# Intuition\nThe problem requires finding the largest positive integer k such that -k also exists in the given array nums. To solve this, we can use a set to efficiently check for the existence of the negation of each number in the array.\n\n# Approach\nInitialize a variable ans to store the result and a set seen to store the numbers encountered so far. Iterate through each number num in the nums array. Check if the negation of the current number -num exists in the seen set. If it does, update ans to the maximum of its current value and the absolute value of num. Otherwise, add num to the seen set. After iterating through all numbers in the array, return ans as the result.\n\n# Complexity\n- Time complexity:\nThe time complexity of this solution is O(n), where n is the length of the input array `nums`. This is because we iterate through the array once, and each set operation (addition and checking for existence) takes O(1) time on average.\n\n- Space complexity:\nThe space complexity is also O(n) because we use a set `seen` to store the unique numbers encountered in the array. In the worst case, the set could contain all elements of the array.\n\n# Code\n```\nclass Solution:\n  def findMaxK(self, nums: List[int]) -> int:\n    ans = -1\n    seen = set()\n\n    for num in nums:\n      if -num in seen:\n        ans = max(ans, abs(num))\n      else:\n        seen.add(num)\n\n    return ans\n```
['Array', 'Hash Table', 'Two Pointers', 'Sorting', 'Python3']