Largest Positive Integer That Exists With Its Negative (LeetCode 2441): Solution and Explanation

Easy
Array
Hash Table
Two Pointers
Sorting

Return the largest positive k such that both k and -k appear in an array with no zeros, or -1 if there is none.

Intuition

We want the biggest value whose mirror image is also present. After sorting, the biggest candidates are at the right end and their possible mirrors are at the left end.

Compare the most negative value a with the largest value b. If a + b < 0, the mirror of a is bigger than anything left, so a can never match and is dropped. If a + b > 0, the mirror of b is smaller than anything left, so b is dropped.

Each comparison removes one value, and the first time the sum is exactly 0 we have the largest k, because every larger positive was already ruled out.

Approach: Sort, then two pointers

  1. Sort nums ascending and set lo = 0, hi = n - 1.
  2. While lo < hi, compute s = nums[lo] + nums[hi].
  3. If s == 0, return nums[hi]. If s < 0, move lo right. Otherwise move hi left.
  4. If the pointers meet, return -1.

Step-by-step example

nums = [-1,10,6,7,-7,1]
  1. Sorted: [-7,-1,1,6,7,10], lo = 0 (-7), hi = 5 (10).
  2. -7 + 10 = 3 > 0: 10 has no mirror, move hi to 7.
  3. -7 + 7 = 0: a mirror pair. Return 7.

Code: Python, Java, C++

1class Solution:
2    def findMaxK(self, nums: List[int]) -> int:
3        nums.sort()
4        lo, hi = 0, len(nums) - 1
5        while lo < hi:
6            s = nums[lo] + nums[hi]
7            if s == 0:
8                return nums[hi]
9            if s < 0:
10                lo += 1
11            else:
12                hi -= 1
13        return -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Hash set O(n)
Boolean array over the value range O(n + V)
Sort, then two pointers O(n log n)
Time

O(n log n)

Sorting dominates; the pointer pass removes one value per step, so it is O(n).

Space

O(1)

Only two pointers, beyond the space used by the sort.

Other approaches

Hash set (time O(n), space O(n))

Put every value in a set, then scan for the largest positive x whose -x is in the set. One pass without sorting.

Boolean array over the value range (time O(n + V), space O(V))

Values are between -1000 and 1000, so mark seen values in an array of size 2001 and check k from 1000 down to 1.

Common mistakes

  • Returning nums[lo] (the negative value) instead of nums[hi].
  • Moving the wrong pointer: a negative sum means the negative side is too large in magnitude, so lo moves.
  • Forgetting the -1 case when the array has only positives, only negatives, or a single element.

Frequently asked questions

Why is the first zero sum the largest answer?

hi only moves left past values that were proved to have no mirror. So when the sum first hits 0, every larger positive has already been ruled out.

Do duplicates break the pointer walk?

No. Duplicates of a value that has no mirror are each dropped in turn, and a duplicated mirror pair is found the first time either copy is compared.

Which approach is better in an interview?

The hash set is O(n) and the simplest to write. The two-pointer version uses O(1) extra space and is a good follow-up when memory matters.