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
.
For example:
nums = [-1,2,-3,3]
Output: 3
Explanation: 3
is the only valid k
we can find in the array.
Another example:
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.
Finally:
nums = [-10,8,6,7,-2,-3]
Output: -1
Explanation: There is no a single valid k
, we return -1
.
Write a function that takes an integer array and returns the largest positive integer k
such that -k
exists in the array. If no such integer exists, return -1
. Can you describe the time and space complexity of your solution?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for this problem is straightforward. We will check every single number in the list to see if its negative counterpart also exists. We will then remember the largest of these positive numbers.
Here's how the algorithm would work step-by-step:
def find_largest_positive_with_negative(numbers):
largest_positive_found = -1
for current_number in numbers:
# Only process if the current number is positive.
if current_number > 0:
negative_counterpart = -current_number
# Check if the negative counterpart exists in the list.
if negative_counterpart in numbers:
# Update the largest positive number if needed.
if current_number > largest_positive_found:
largest_positive_found = current_number
return largest_positive_found
The goal is to find the largest positive number that also has its negative counterpart in the list. We want to avoid checking every single possible number by focusing on finding the biggest positive numbers first and quickly checking if their negative exists. This approach allows us to stop as soon as we find the answer.
Here's how the algorithm would work step-by-step:
def find_largest_positive_with_negative(numbers):
numbers.sort(reverse=True)
number_set = set(numbers)
for number in numbers:
# Only process positive numbers
if number > 0:
negative_number = -number
# Check if the negative counterpart exists
if negative_number in number_set:
return number
# No such number exists
return -1
Case | How to Handle |
---|---|
Empty or null input array | Return -1 immediately as no positive integer K can be found in an empty array. |
Array with only one element | Return -1 since we need both K and -K, requiring at least two elements. |
Array contains only positive numbers | Return -1 since no negative counterpart exists for any positive number. |
Array contains only negative numbers | Return -1 since no positive number exists. |
Array contains zero | Zero does not contribute to finding a positive integer K and its negative -K, so it's ignored. |
Array contains duplicates of K and -K | The algorithm should correctly identify K as a valid solution, even with duplicates of K and -K present. |
Large input array with potential integer overflow during calculations | Use appropriate data types (e.g., long) or avoid calculations that could lead to overflow; if overflow occurs, catch the exception and handle it gracefully, returning -1 if necessary. |
No valid solution exists | Return -1 when no integer K and its negative -K are found in the array after iterating through all the numbers. |