Skip to main content

Find Closest Number to Zero - Solution & Explanation

EasyArray12 min readAsked at: Amazon, Microsoft, Meta +3
Practice this problem

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(1)

Try this approach in the editor β†’

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(n) (due to call stack)

Try this approach in the editor β†’

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

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
Approach 1: Iterative Solution with Array Traversal

Time Complexity: O(n)
Space Complexity: O(1)

Approach 2: Recursive Solution

Time Complexity: O(n)
Space Complexity: O(n) (due to call stack)

Single Passβ€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Array TraversalO(n)O(1)Best general solution. Minimal memory usage and a single pass over the array.
Recursive ComparisonO(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 is classified as an Easy problem. The challenge focuses on careful comparison logic and handling the tie-breaking rule when two numbers are equally distant from zero.
Find Closest Number to Zero Python/Java solution
In Python or Java, the solution iterates through the array and compares absolute values using Math.abs() or abs(). Update the stored result when a number is closer to zero or when two values tie but the new number is larger. The implementation runs in O(n) time with constant space.
How to solve Find Closest Number to Zero in O(n)?
Iterate through the array once while maintaining a variable storing the closest value found so far. For each number, compare abs(num) with abs(currentBest). Update the result if the new number is closer to zero or if the absolute values are equal but the number is larger. This produces the correct answer in a single O(n) pass.
What is the best approach for Find Closest Number to Zero?
The best approach is a single-pass iterative traversal of the array. Track the current closest value and update it when you find a number with a smaller absolute distance from zero. If two numbers have the same absolute value, choose the larger number. This method runs in O(n) time and uses O(1) extra space.
Is Find Closest Number to Zero asked at Google/Amazon/Meta?
Problems involving array traversal and comparison logic appear frequently in interviews at companies like Amazon, Google, and Meta. While this exact problem may vary slightly, the pattern of scanning an array and tracking a best candidate is a common interview exercise.
What data structure is used in Find Closest Number to Zero?
The problem primarily uses a simple array traversal. No additional data structures such as hash maps, heaps, or trees are required. A single variable is enough to track the current closest number during iteration.
What is the time complexity of Find Closest Number to Zero?
The optimal solution runs in O(n) time because every element in the array must be examined at least once. Each step performs constant-time operations such as absolute value calculation and comparisons. Space complexity is O(1) when implemented iteratively.

Ready to solve this problem?

Practice Find Closest Number to Zero with our built-in code editor and test cases.

Practice on FleetCode