Find Closest Number to Zero (LeetCode 2239): Solution and Explanation
Return the element of an integer array that is closest to zero. When a negative and a positive number are equally close, return the positive one.
Intuition
How close a number is to zero is just its absolute value, so the task is to find the element with the smallest |x|.
That is a running minimum. Keep the best element seen so far and replace it whenever a new element is strictly closer.
The only twist is the tie. Two different values can only be equally close when one is x and the other is -x. The rule asks for the larger value, so on a tie the positive number wins.
Picture a window [-|best|, |best|] around zero. Any element that lands strictly inside it is closer than best, and the window shrinks every time best improves.
Approach: Running best with a tie-break
- Set
best = nums[0]. - For each
xinnums, check whetherabs(x) < abs(best). - If the absolute values are equal, check whether
x > bestinstead. - Replace
bestwithxwhen either check passes, and returnbestat the end.
Step-by-step example
nums = [-4,-2,1,4,8]
- Start with best = -4, so the window around zero is [-4, 4].
- x = -2 has distance 2, which is less than 4, so best = -2 and the window shrinks to [-2, 2].
- x = 1 has distance 1, which is less than 2, so best = 1 and the window shrinks to [-1, 1].
- x = 4 and x = 8 are farther away, so best stays 1. Return 1.
Code: Python, Java, C++
1class Solution:
2 def findClosestNumber(self, nums: List[int]) -> int:
3 best = nums[0]
4 for x in nums:
5 if abs(x) < abs(best) or (abs(x) == abs(best) and x > best):
6 best = x
7 return best1class Solution {
2 public int findClosestNumber(int[] nums) {
3 int best = nums[0];
4 for (int x : nums) {
5 if (Math.abs(x) < Math.abs(best) || (Math.abs(x) == Math.abs(best) && x > best)) {
6 best = x;
7 }
8 }
9 return best;
10 }
11}1class Solution {
2public:
3 int findClosestNumber(vector<int>& nums) {
4 int best = nums[0];
5 for (int x : nums) {
6 if (abs(x) < abs(best) || (abs(x) == abs(best) && x > best)) {
7 best = x;
8 }
9 }
10 return best;
11 }
12};Time and space complexity
O(n)
Each element is compared once against the current best.
O(1)
Only the current best is stored.
Other approaches
Sort by key (time O(n log n), space O(n))
Sort by (abs(x), -x) and take the first element. It is short to write but costs a sort for a problem that only needs a minimum.
Min with a key (time O(n), space O(1))
Python's min(nums, key=lambda x: (abs(x), -x)) does the same one-pass scan in a single line.
Common mistakes
- Forgetting the tie-break. With
[-1, 1]a plainabs(x) < abs(best)check keeps -1, but the answer is 1. - Returning the distance
abs(best)instead of the element itself. - Starting
bestat 0 or at a large constant instead of a real element. Start fromnums[0]so the answer is always an element of the array. - Writing the tie check as
x >= best. It still works, butx > bestmakes the intent clear.
Frequently asked questions
When can two different numbers be equally close to zero?
Only when they are opposites, like -3 and 3. Then the positive one is larger, so it is the answer.
Does the order of the array matter?
No. The comparison picks the same winner regardless of where the elements appear.
What if the array contains 0?
Then 0 is the answer. Its distance is 0, and no other value can tie it.