NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts
  • 746. Min Cost Climbing StairsEasy
  • 1480. Running Sum of 1d ArrayEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy
  • 26. Remove Duplicates from Sorted ArrayEasy

2239. Find Closest Number to Zero

Easy
Mid LevelArrayBiweekly Contest 76
Answered: Feb 24, 2026
View on LeetCode

Problem Description

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.

Examples

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

Solutions

Complexity Analysis

Time Complexity:O(n)

The algorithm iterates through the array once, so the time complexity is linear in the size of the input array.

Space Complexity:O(1)

The algorithm uses a constant amount of extra space to store the closest number, regardless of the input size.

The algorithm iterates through the array once, keeping track of the closest number to zero found so far. For each number, it compares its absolute value to the absolute value of the closest number. If it's closer to zero, it updates the closest number. If it's equally close to zero but has a larger value, it also updates the closest number. This ensures that in case of a tie (e.g., -1 and 1), the larger value (1) is returned.

Complexity Analysis

Time Complexity:O(n)

The algorithm iterates through the array twice in the worst case: once to find the closest number and once to check if a negative number with the same absolute value exists. Since both iterations are linear, the overall time complexity is O(n)O(n)O(n).

Space Complexity:O(1)

The algorithm uses a constant amount of extra space to store temporary variables, regardless of the input size.

This approach first finds the number closest to zero in a single pass, then checks if there's a negative number with the same absolute value in the array. If such a number exists, it returns the positive version of that number. Otherwise, it returns the closest number found. This ensures that if there are two numbers equally close to zero (e.g., -1 and 1), the larger value (1) is returned.

Conclusion

ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
Single-pass with Tie-breaking
O(n)O(n)O(n)O(1)O(1)O(1)Handles tie-breaking inline during the scanSlightly more complex condition checks
Linear Scan with Post-processing
O(n)O(n)O(n)O(1)O(1)O(1)Clean separation of concerns — find closest first, then handle tiesMay iterate the array twice in worst case (still O(n)O(n)O(n))

The optimal approach for this problem is Solution 2: Linear Scan with Post-processing, which achieves O(n)O(n)O(n) time complexity and O(1)O(1)O(1) space complexity. It cleanly separates the logic into two steps: first finding the closest number, then handling the tie-breaking case where a negative number's positive counterpart exists in the array.


Previous1480. Running Sum of 1d Array
Related posts
  • 746. Min Cost Climbing StairsEasy
  • 1480. Running Sum of 1d ArrayEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy
  • 26. Remove Duplicates from Sorted ArrayEasy
On this page
12 lines
1class Solution:
2 def findClosestNumber(self, nums: List[int]) -> int:
3 closest = nums[0]
4
5 for i in range(1, len(nums)):
6 # Check if the current number is closer to zero than the closest number found so far
7 if abs(nums[i]) < abs(closest):
8 closest = nums[i]
9 elif abs(nums[i]) == abs(closest) and nums[i] > closest: # If the current number is equally close to zero as the closest number, but has a larger value, update closest
10 closest = nums[i]
11
12 return closest
14 lines
1class Solution:
2 def findClosestNumber(self, nums: List[int]) -> int:
3 closest = nums[0]
4
5 for i in range(1, len(nums)):
6 # Check if the current number is closer to zero than the closest number found so far
7 if abs(nums[i]) < abs(closest):
8 closest = nums[i]
9
10 # Check if the closest number is negative and its absolute value exists in the array
11 if closest < 0 and abs(closest) in nums:
12 return abs(closest)
13 else:
14 return closest