Find Closest Number to Zero

Easy

Description

Given an integer array nums of size n, return the number with the value closest to 0. If there are multiple answers with the same distance, return the positive number.

Examples

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

1 is closest to 0.

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

Both -1 and 1 are distance 1 from zero, and 2 is distance 2. Since -1 and 1 tie, 1 is chosen as the positive number.

Input:nums = [-3,3]
Output:3
Explanation:

Both -3 and 3 are equally distant from 0 (distance = 3), so the positive number 3 is chosen.

Constraints

  • 1 ≤ nums.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!