2441. Largest Positive Integer That Exists With Its Negative LeetCode Solution
In this guide, you will get 2441. Largest Positive Integer That Exists With Its Negative LeetCode Solution with the best time and space complexity. The solution to Largest Positive Integer That Exists With Its Negative problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Largest Positive Integer That Exists With Its Negative solution in C++
- Largest Positive Integer That Exists With Its Negative solution in Java
- Largest Positive Integer That Exists With Its Negative solution in Python
- Additional Resources

Problem Statement of Largest Positive Integer That Exists With Its Negative
Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.
Return the positive integer k. If there is no such integer, return -1.
Example 1:
Input: nums = [-1,2,-3,3]
Output: 3
Explanation: 3 is the only valid k we can find in the array.
Example 2:
Input: nums = [-1,10,6,7,-7,1]
Output: 7
Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
Example 3:
Input: nums = [-10,8,6,7,-2,-3]
Output: -1
Explanation: There is no a single valid k, we return -1.
Constraints:
1 <= nums.length <= 1000
-1000 <= nums[i] <= 1000
nums[i] != 0
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2441. Largest Positive Integer That Exists With Its Negative LeetCode Solution in C++
class Solution {
public:
int findMaxK(vector<int>& nums) {
int ans = -1;
unordered_set<int> seen;
for (const int num : nums)
if (seen.contains(-num))
ans = max(ans, abs(num));
else
seen.insert(num);
return ans;
}
};
/* code provided by PROGIEZ */
2441. Largest Positive Integer That Exists With Its Negative LeetCode Solution in Java
class Solution {
public int findMaxK(int[] nums) {
int ans = -1;
Set<Integer> seen = new HashSet<>();
for (final int num : nums)
if (seen.contains(-num))
ans = Math.max(ans, Math.abs(num));
else
seen.add(num);
return ans;
}
}
// code provided by PROGIEZ
2441. Largest Positive Integer That Exists With Its Negative LeetCode Solution in Python
class Solution:
def findMaxK(self, nums: list[int]) -> int:
ans = -1
seen = set()
for num in nums:
if -num in seen:
ans = max(ans, abs(num))
else:
seen.add(num)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.