Find Closest Number to Zero
EasyUpdated: Aug 2, 2025
Practice on:
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
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-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
- 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. - Iterate through each number in the array:
- If the absolute value of the current number is less than the absolute value of
ans, updateans. - If the absolute values are equal, update
ansonly if the current number is greater thanans.
- If the absolute value of the current number is less than the absolute value of
- Return
ansafter the loop.
Code
C++
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;
}
};
Go
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
}
Java
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;
}
}
Kotlin
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
}
}
Python
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
Rust
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
}
}
TypeScript
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.