Problem

Given an integer array nums of size n, return _the number with the valueclosest to _0 innums. If there are multiple answers, return the number with thelargest value.

Examples

Example 1

1
2
3
4
5
6
7
8
9
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

1
2
3
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
  • -10^5 <= nums[i] <= 10^5

Solution

Method 1 – Linear Scan 1

Intuition

The closest number to zero is the one with the smallest absolute value. If two numbers are equally close, the larger one should be chosen.

Approach

  1. Initialize a variable to store the answer (e.g., ans) and set it to a value that will be replaced by any number in the array.
  2. Iterate through each number in the array:
    • If the absolute value of the current number is less than the absolute value of ans, update ans.
    • If the absolute values are equal, update ans only if the current number is greater than ans.
  3. Return ans after the loop.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    int findClosestNumber(vector<int>& nums) {
        int ans = nums[0];
        for (int n : nums) {
            if (abs(n) < abs(ans) || (abs(n) == abs(ans) && n > ans)) {
                ans = n;
            }
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
func findClosestNumber(nums []int) int {
    ans := nums[0]
    for _, n := range nums {
        if abs(n) < abs(ans) || (abs(n) == abs(ans) && n > ans) {
            ans = n
        }
    }
    return ans
}
func abs(x int) int {
    if x < 0 {
        return -x
    }
    return x
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    public int findClosestNumber(int[] nums) {
        int ans = nums[0];
        for (int n : nums) {
            if (Math.abs(n) < Math.abs(ans) || (Math.abs(n) == Math.abs(ans) && n > ans)) {
                ans = n;
            }
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    fun findClosestNumber(nums: IntArray): Int {
        var ans = nums[0]
        for (n in nums) {
            if (kotlin.math.abs(n) < kotlin.math.abs(ans) || (kotlin.math.abs(n) == kotlin.math.abs(ans) && n > ans)) {
                ans = n
            }
        }
        return ans
    }
}
1
2
3
4
5
6
7
class Solution:
    def findClosestNumber(self, nums: list[int]) -> int:
        ans = nums[0]
        for n in nums:
            if abs(n) < abs(ans) or (abs(n) == abs(ans) and n > ans):
                ans = n
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
impl Solution {
    pub fn find_closest_number(nums: Vec<i32>) -> i32 {
        let mut ans = nums[0];
        for &n in &nums {
            if n.abs() < ans.abs() || (n.abs() == ans.abs() && n > ans) {
                ans = n;
            }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    findClosestNumber(nums: number[]): number {
        let ans = nums[0];
        for (const n of nums) {
            if (Math.abs(n) < Math.abs(ans) || (Math.abs(n) === Math.abs(ans) && n > ans)) {
                ans = n;
            }
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), as we scan the array once.
  • 🧺 Space complexity: O(1), as only a constant amount of extra space is used.