Given an integer array nums
of size n
, return the number with the value closest to 0
in nums
. If there are multiple answers, return the number with the largest value.
Example 1:
Input: nums = [-4,-2,1,4,8] Output: 1 Explanation: The distance from -4 to 0 is |-4| = 4. The distance from -2 to 0 is |-2| = 2. The distance from 1 to 0 is |1| = 1. The distance from 4 to 0 is |4| = 4. The distance from 8 to 0 is |8| = 8. Thus, the closest number to 0 in the array is 1.
Example 2:
Input: nums = [2,-1,1] Output: 1 Explanation: 1 and -1 are both the closest numbers to 0, so 1 being larger is returned.
Constraints:
1 <= n <= 1000
-105 <= nums[i] <= 105
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 approach to finding the number closest to zero involves checking every single number in the given set. We'll compare the distance of each number from zero to find the smallest distance.
Here's how the algorithm would work step-by-step:
def find_closest_to_zero_brute_force(numbers):
closest_number = numbers[0]
smallest_distance = abs(numbers[0])
for number in numbers:
current_distance = abs(number)
# Check if current distance is smaller
if current_distance < smallest_distance:
smallest_distance = current_distance
closest_number = number
# Handle the case where distances are equal
elif current_distance == smallest_distance:
# Prefer the positive number
if number > closest_number:
closest_number = number
return closest_number
The fastest way to find the number closest to zero is to focus on finding the smallest positive and largest negative numbers. We only need to consider these two values and compare their distances to zero. This is much better than checking every number in the list.
Here's how the algorithm would work step-by-step:
def find_closest_to_zero(numbers):
smallest_positive = float('inf')
largest_negative = float('-inf')
for number in numbers:
if number > 0:
smallest_positive = min(smallest_positive, number)
elif number < 0:
largest_negative = max(largest_negative, number)
# Handle the case where no positive or negative numbers exist
if smallest_positive == float('inf') and largest_negative == float('-inf'):
return 0
elif smallest_positive == float('inf'):
return largest_negative
elif largest_negative == float('-inf'):
return smallest_positive
positive_distance = abs(smallest_positive)
negative_distance = abs(largest_negative)
# Need to compare the absolute values to find closest.
if positive_distance < negative_distance:
return smallest_positive
# If distances are equal, positive number is returned per instructions
else:
return smallest_positive
Case | How to Handle |
---|---|
Empty or null input array | Return 0 or throw an exception, as the problem doesn't specify, but the solution must explicitly handle this case. |
Array with only one element | Return that single element as it's trivially the closest to zero. |
Array contains only zero | Return 0 directly, which is the closest number to zero. |
Array contains both positive and negative numbers equidistant from zero, like [-1, 1, 2] | Prioritize returning the positive number as stated in the problem description. |
Array contains duplicate numbers, some positive, some negative, e.g. [-2, -1, 1, 2] | Ensure the algorithm correctly identifies the closest, choosing positive if equally distant. |
Array contains maximum and minimum integer values (Integer.MAX_VALUE, Integer.MIN_VALUE) | Consider potential overflow if calculating absolute values or distances, and use long if necessary. |
Array with all negative numbers | The algorithm should still correctly identify the number closest to zero from the negative numbers. |
Large input array (scalability) | A linear time solution (O(n)) is expected; ensure the algorithm doesn't have hidden quadratic complexity. |