Largest Positive Integer That Exists With Its Negative (LeetCode 2441): Solution and Explanation
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
- Sort nums ascending and set lo = 0, hi = n - 1.
- While lo < hi, compute s = nums[lo] + nums[hi].
- If s == 0, return nums[hi]. If s < 0, move lo right. Otherwise move hi left.
- If the pointers meet, return -1.
Step-by-step example
nums = [-1,10,6,7,-7,1]
- Sorted: [-7,-1,1,6,7,10], lo = 0 (-7), hi = 5 (10).
- -7 + 10 = 3 > 0: 10 has no mirror, move hi to 7.
- -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 -11class Solution {
2 public int findMaxK(int[] nums) {
3 Arrays.sort(nums);
4 int lo = 0, hi = nums.length - 1;
5 while (lo < hi) {
6 int s = nums[lo] + nums[hi];
7 if (s == 0) return nums[hi];
8 if (s < 0) {
9 lo++;
10 } else {
11 hi--;
12 }
13 }
14 return -1;
15 }
16}1class Solution {
2public:
3 int findMaxK(vector<int>& nums) {
4 sort(nums.begin(), nums.end());
5 int lo = 0, hi = (int)nums.size() - 1;
6 while (lo < hi) {
7 int s = nums[lo] + nums[hi];
8 if (s == 0) return nums[hi];
9 if (s < 0) {
10 lo++;
11 } else {
12 hi--;
13 }
14 }
15 return -1;
16 }
17};Time and space complexity
O(n log n)
Sorting dominates; the pointer pass removes one value per step, so it is O(n).
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.