Return the number whose value is closest to zero, breaking ties by choosing the larger number.
Given an integer array, find the element whose absolute value is smallest. If there is a tie, return the larger value.
You may assume the array contains at least one integer.
Scan the array and return the closest number to zero according to the rule above.
nums of length at least 1.Example 1
Input
nums = [-4, -2, 1, 4, 8]
Output
1
Explanation
The absolute values are 4, 2, 1, 4, and 8. The smallest is 1, so the answer is 1.
Example 2
Input
nums = [2, -1, 1]
Output
1
Explanation
Both -1 and 1 are equally close to zero, but 1 is larger, so return 1.
Example 3
Input
nums = [0, 5, -3]
Output
0
Explanation
Zero is the closest possible value to zero.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.