Leetcode: 2441. Largest Positive Integer That Exists With Its Negative
Understand the problem
Given an array \(a\), find the largest integer \(k\) such that \(-k\) is also in \(a\).
Useful prompts
Devise a plan
Use a set
to store numbers of \(a\) and for each one \(x\) from the largest to the smaller, check if \(-x\) is also in the set.
Carry out the plan
class Solution: def findMaxK(self, nums: List[int]) -> int: seen = set(nums) for n in sorted(nums, reverse=True): if -n in seen: return n return -1