Find Closest Number to Zero - Solution & Explanation
Problem Statement
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
Approach Overview
Problem Overview: You receive an integer array and must return the value closest to 0. If two numbers are equally close (for example -3 and 3), return the larger number. The task is essentially scanning the array and tracking the element with the smallest absolute distance from zero.
Approach 1: Iterative Solution with Array Traversal (Time: O(n), Space: O(1))
The most direct approach is a single pass through the array. Maintain a variable that stores the current best candidate. For each element, compute abs(num) and compare it with the absolute value of the current answer. If the new number is closer to zero, update the result. If the absolute values are equal, choose the larger number to satisfy the tie-breaking rule. This method relies only on sequential iteration over the array, performs constant-time comparisons, and requires no additional data structures. Since every element is visited exactly once, the time complexity is O(n) with O(1) extra space.
Approach 2: Recursive Solution (Time: O(n), Space: O(n))
A recursive variant processes one element per function call and passes the current best result forward. Each recursive step compares the current element with the best candidate returned from the remaining subarray. The comparison logic remains identical: evaluate absolute distance from zero and resolve ties by picking the larger number. While the algorithm still examines each element once, recursion introduces call stack overhead proportional to the array length. That results in O(n) time and O(n) auxiliary space due to the recursion stack. This version mainly demonstrates recursion patterns rather than improving performance.
Recommended for interviews: The iterative traversal is the expected answer. Interviewers want to see that you immediately recognize the problem as a linear scan over an array with constant state tracking. Mentioning the tie-breaking condition explicitly and implementing it correctly shows attention to detail. The recursive solution works but adds unnecessary stack overhead, so itβs rarely preferred unless the discussion focuses on recursion techniques.
Approach 1: Approach 1: Iterative Solution with Array Traversal
This approach involves iteratively traversing the array or list to find the solution. It's important to use a loop to iterate through the elements, maintaining variables to track the current state, and update them as needed. This approach is typical for problems such as finding maximums, minimums, or accumulating values.
This C program calculates the sum of an array using an iterative approach. It loops through each array element, accumulating the total.
Complexity
Time Complexity: O(n)
Space Complexity: O(1)
Approach 2: Approach 2: Recursive Solution
This approach uses recursion to solve the problem by breaking it down into sub-problems. Recursive solutions can be elegant and succinct but may not always be optimal for large input sizes due to stack overflow risks and lack of tail-call optimization in many languages.
This solution uses recursion to sum an array in C. The base case checks if there are no elements left, and the recursive case adds the last element to the rest.
Complexity
Time Complexity: O(n)
Space Complexity: O(n) (due to call stack)
Approach 3: Single Pass
We define a variable d to record the current minimum distance, initially 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 ans, we update the answer ans=x and d=y.
After the traversal, return the answer.
The time complexity is O(n), where n is the length of the array. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Approach 1: Iterative Solution with Array Traversal | Time Complexity: O(n) |
| Approach 2: Recursive Solution | Time Complexity: O(n) |
| Single Pass | β |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Array Traversal | O(n) | O(1) | Best general solution. Minimal memory usage and a single pass over the array. |
| Recursive Comparison | O(n) | O(n) | Useful for demonstrating recursion patterns or practicing divide-and-conquer style traversal. |
Video Solution
Find Closest Number to Zero - Leetcode 2239 - Arrays & Strings (Python) β’ Greg Hogg β’ 89,224 views views
Watch 9 more video solutions βFrequently Asked Questions
Is Find Closest Number to Zero easy or hard?
Find Closest Number to Zero Python/Java solution
How to solve Find Closest Number to Zero in O(n)?
What is the best approach for Find Closest Number to Zero?
Is Find Closest Number to Zero asked at Google/Amazon/Meta?
What data structure is used in Find Closest Number to Zero?
What is the time complexity of Find Closest Number to Zero?
Ready to solve this problem?
Practice Find Closest Number to Zero with our built-in code editor and test cases.
Practice on FleetCode