Watch 10 video solutions for Find Closest Number to Zero, a easy level problem involving Array. This walkthrough by Greg Hogg has 83,960 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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] <= 105Problem 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 | 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. |