Find Closest Number to Zero

  • 19 Jul, 2025

๐Ÿ“„ Problem Statement

โœ… Problem: Find Closest Number to Zero

Given an integer array nums, return the number with the value closest to 0.

Note:

  • If there are multiple answers, return the number with the largest value.
  • Use absolute value to determine closeness.

Example 1:

  • Input: nums = [-4, -2, 1, 4, 8]
  • Output: 1

Example 2:

  • Input: nums = [2, -1, 1]
  • Output: 1

Constraints:

  • 1 <= nums.length <= 1000
  • -10^5 <= nums[i] <= 10^5

๐Ÿง  Problem Explanation

Given an integer array nums, return the number with the value closest to 0.

If there are multiple answers (e.g., -x and x), return the number with the larger value (i.e., the positive one).

๐Ÿ’ก Approach

  • Use a linear scan to go through the array.
  • Track the number with the smallest absolute value.
  • If two numbers are equally close to 0, pick the positive one.

๐Ÿ“Š Complexities

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

๐Ÿงพ Pseudocode

function: findClosestNumber
params:
  - nums: List[int]
logic:
  - initialize closest with nums[0]
  - for each num in nums:
      - if abs(num) < abs(closest):
          - set closest = num
      - else if abs(num) == abs(closest) and num > closest:
          - set closest = num
  - return closest

โœ… Code

class Solution(object):
    def findClosestNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        number = nums[0]
        for i in nums:
            if abs(i) < abs(number):
                number = i

        if abs(number) in nums:
            return abs(number)
        else:
            return number

๐Ÿงช Example Tests

  • Input: [-4, -2, 1, 4, 8]
    Expected Output: 1

  • Input: [2, -1, 1]
    Expected Output: 1

๐Ÿ“ Notes

  • Pattern Used: Linear Scan
  • Insights:
    • I came up with a solution intuitively using a brute-force approach. Since the time complexity was acceptable, I decided to proceed with it.
    • How to use abs() for comparison and handle tie-breaking effectively.