Largest Positive Integer That Exists With Its Negative
We need to find the largest positive integer k
in the list such that k
is also in the list. If there is no such integer, we need to return 1
.
The key to solve this problem is to make use of a set
data structure. We will follow these steps:
 Initialize a variable
max_k
to1
, which will store the maximum positivek
found so far.  Initialize an empty
set
namednum_set
to store the numbers.  Iterate over each number in
nums
. If the negative of the current number is in
num_set
and the absolute value of current number is greater thanmax_k
, updatemax_k
with the absolute value of the current number.  Add the current number to
num_set
.
 If the negative of the current number is in
 After the loop, return
max_k
.
Here is the Python code:


This algorithm works because it checks for each number in nums
if its negation is in the set
. If it is, it checks if the absolute value of this number is the largest found so far. In the end, it will return the largest k
if found, or 1
if not. The time complexity of this solution is O(n), where n is the length of the nums
list. The space complexity is also O(n), for storing the numbers in a set
.