↓Skip to main content
  1. LeetCode/

LeetCode 2441: Largest Positive Integer That Exists With Its Negative

·2 mins· ·
LeetCode Easy Hash-Map Two-Pointers Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-08-06

  • Submissions: Wrong Answer ×4 → Accepted

Approach
#

Find the largest positive k such that -k is also in the array.

Put positives and absolute values of negatives into two separate sets; values in both are valid k, and the answer is the largest.

My Attempts
#

Attempts 1 and 2: absolute values only
#

for num in nums:
    if num < 0:
        num *= -1
    if num not in exist:
        exist.add(num)
    else:
        max_num = max(max_num, num)

Every number became its absolute value, and a second sighting counted as a pair. The problem: same-sign duplicates also count, so [3, 3] returns 3 even though -3 is not in the array. The second attempt only added “return -1 when nothing is found” and did not fix this.

Attempts 3 and 4: two sets, signs mixed up
#

Next I used one set for positives and one for negatives, but the signs were never handled right: the third attempt put raw negatives into exist_pos and positives into exist_neg, so one set held only negatives and the other only positives and the intersection was always empty; the fourth negated the positives, so both sets held negatives and the intersection was found, but it returned -k. The final version stores “negatives as absolute values in exist_neg, positives as-is in exist_pos” and passed.

Solution
#

class Solution:
    def findMaxK(self, nums: List[int]) -> int:
        pos = {x for x in nums if x > 0}
        neg = {-x for x in nums if x < 0}
        both = pos & neg
        return max(both) if both else -1

Alternative: sort and use two pointers
#

After sorting, start the left pointer at the smallest negative and the right pointer at the largest positive: equal absolute values give the answer; if the left side’s absolute value is larger, move left rightwards, otherwise move right leftwards. Time O(n log n), space O(1).

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Looking only at absolute values: same-sign duplicates are mistaken for pairs.
  • Inconsistent signs between the two sets: the intersection is empty, or the answer comes back negative.

Interview Explanation
#

I put positives in one set and absolute values of negatives in another; their intersection is the numbers present with both signs, and I return the largest, or -1 if it is empty. Absolute values alone do not work, because same-sign duplicates would look like a pair. Time O(n).

Related

LeetCode 977: Squares of a Sorted Array
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 977 note: the largest squares are at the two ends, so compare the ends with two pointers and fill the result from the back
LeetCode 905: Sort Array By Parity
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 905 note: a write pointer swaps even numbers to the front, just like move zeroes
LeetCode 922: Sort Array By Parity II
·1 min
LeetCode Easy Array Two-Pointers Sorting
LeetCode 922 note: pointers i over even slots and j over odd slots, each stepping by two; when slot i holds an odd number, move j to a misplaced even and swap
LeetCode 455: Assign Cookies
·2 mins
LeetCode Easy Array Two-Pointers Greedy Sorting
LeetCode 455 note: sort children and cookies, satisfying the least greedy child with the smallest cookie that suffices; each pointer walks once
LeetCode 3606: Coupon Code Validator
·2 mins
LeetCode Easy Hash-Table String Sorting
LeetCode 3606 note: filter valid coupons, then sort by (category order, code); character ranges must include z, Z and 9
LeetCode 3536: Maximum Product of Two Digits
·2 mins
LeetCode Easy Math Sorting
LeetCode 3536 note: multiply the largest and second-largest digits; when a new maximum appears, the old maximum must move to second place