LeetCode Problem Workspace
Find Closest Number to Zero
Identify the number in an integer array that is closest to zero, returning the larger one if tied.
1
Topics
5
Code langs
3
Related
Practice Focus
Easy · Array-driven solution strategy
Answer-first summary
Identify the number in an integer array that is closest to zero, returning the larger one if tied.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array-driven solution strategy
The fastest way is to iterate through the array, tracking the current closest number to zero. Compare absolute values, updating the candidate whenever a smaller distance is found, and choose the larger number if there is a tie. This direct array-driven strategy avoids unnecessary sorting or extra data structures while ensuring correctness.
Problem Statement
Given an integer array nums, return the number whose value is nearest to zero. If multiple numbers are equally close, return the largest among them.
For example, in nums = [-4,-2,1,4,8], the closest number to zero is 1. In nums = [2,-1,1], both 1 and -1 are closest to zero, so 1 is returned because it is larger.
Examples
Example 1
Input: nums = [-4,-2,1,4,8]
Output: 1
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
1 and -1 are both the closest numbers to 0, so 1 being larger is returned.
Constraints
- 1 <= n <= 1000
- -105 <= nums[i] <= 105
Solution Approach
Linear Scan Tracking
Initialize a variable to store the closest number. Iterate through each element, updating it if the absolute value is smaller, or if tied, the element is larger.
Compare Absolute Values
During iteration, use absolute values to determine proximity to zero. Directly comparing abs(nums[i]) ensures that you correctly handle negative numbers.
Avoid Sorting
Sorting the array is unnecessary and adds O(n log n) overhead. A single pass with comparisons achieves O(n) time with minimal space usage.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) since we examine each array element once. Space complexity is O(1) because only a single variable is needed to track the closest number.
What Interviewers Usually Probe
- Candidate considers edge cases with negative and positive ties.
- Candidate directly compares absolute values without extra sorting.
- Candidate handles arrays with size 1 or repeated elements correctly.
Common Pitfalls or Variants
Common pitfalls
- Forgetting to return the larger number in a tie.
- Using sorting unnecessarily, increasing time complexity.
- Neglecting negative numbers and comparing raw values instead of absolute values.
Follow-up variants
- Find the closest number to a given target instead of zero.
- Return all numbers equally closest to zero.
- Handle arrays with floating point numbers instead of integers.
FAQ
What is the best strategy for Find Closest Number to Zero?
Use a linear scan through the array, track the closest number using absolute values, and choose the larger if tied.
Can I sort the array first?
Sorting is not required and adds extra time; a single-pass approach is faster and simpler.
How do I handle negative numbers?
Compare absolute values to correctly determine which number is closer to zero.
What if multiple numbers are equally close?
Return the largest number among them to satisfy the problem condition.
Does the array-driven solution pattern always work?
Yes, for this problem, iterating through the array while tracking the closest value ensures correctness and efficiency.
Solution
Solution 1: Single Pass
We define a variable $\textit{d}$ to record the current minimum distance, initially $\textit{d}=\infty$. Then we traverse the array, for each element $x$, we calculate $y=|x|$. If $y \lt d$ or $y=d$ and $x \gt \textit{ans}$, we update the answer $\textit{ans}=x$ and $\textit{d}=y$.
class Solution:
def findClosestNumber(self, nums: List[int]) -> int:
ans, d = 0, inf
for x in nums:
if (y := abs(x)) < d or (y == d and x > ans):
ans, d = x, y
return ansContinue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward