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.
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.
Time Complexity: O(n)
Space Complexity: O(1)
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.
Time Complexity: O(n)
Space Complexity: O(n) (due to call stack)
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).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Iterative Solution with Array Traversal | Time Complexity: O(n) |
| Approach 2: Recursive Solution | Time Complexity: O(n) |
| Single Pass | — |
| 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. |
Find Closest Number to Zero - Leetcode 2239 - Arrays & Strings (Python) • Greg Hogg • 83,960 views views
Watch 9 more video solutions →Practice Find Closest Number to Zero with our built-in code editor and test cases.
Practice on FleetCode