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:
1Explanation:
Distances from zero: |-4|=4, |-2|=2, |1|=1, |4|=4, |8|=8. The minimum distance is 1, so the answer is 1.
Input:
nums = [2,-1,1]Output:
1Explanation:
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:
3Explanation:
Both -3 and 3 are equally distant from 0 (distance = 3), so the positive number 3 is chosen.
Constraints
- •
1 ≤ nums.length ≤ 1000