2441. Largest Positive Integer That Exists With Its Negative

Updated: 2024-06-02
1 min read
[]

On This Page

LeetCode problem 2441

class Solution:
    def findMaxK(self, nums: List[int]) -> int:
        nums.sort()
        p1 = 0
        p2 = len(nums)-1

        while p1 < p2:
            l = nums[p1]
            r = nums[p2]

            ssum = l + r
            if ssum == 0:
                return r
            if ssum < 0:
                p1 += 1
            else:
                p2 -= 1
        
        return -1
class Solution:
    def findMaxK(self, nums: List[int]) -> int:
        s = set(nums)
        return max((x for x in s if -x in s), default=-1)