Find Closest Number to Zero (LeetCode 2239): Solution and Explanation

Easy
Array

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

  1. Set best = nums[0].
  2. For each x in nums, check whether abs(x) < abs(best).
  3. If the absolute values are equal, check whether x > best instead.
  4. Replace best with x when either check passes, and return best at the end.

Step-by-step example

nums = [-4,-2,1,4,8]
  1. Start with best = -4, so the window around zero is [-4, 4].
  2. x = -2 has distance 2, which is less than 4, so best = -2 and the window shrinks to [-2, 2].
  3. x = 1 has distance 1, which is less than 2, so best = 1 and the window shrinks to [-1, 1].
  4. 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 best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Running best with a tie-break O(n)
Min with a key O(n)
Sort by key O(n log n)
Time

O(n)

Each element is compared once against the current best.

Space

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 plain abs(x) < abs(best) check keeps -1, but the answer is 1.
  • Returning the distance abs(best) instead of the element itself.
  • Starting best at 0 or at a large constant instead of a real element. Start from nums[0] so the answer is always an element of the array.
  • Writing the tie check as x >= best. It still works, but x > best makes 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.