Given an integer array `nums` of size `n`, return the number with the value closest to `0` in `nums`. If there are multiple answers, return the number with the largest value.
Given an integer array nums of size n, return the number with the value closest to 0 in nums. If there are multiple answers, return the number with the largest value.
Example 1:
Input: {"nums":[-4,-2,1,4,8]}
Output: 1
Constraints:
1≤n≤1000
−105≤nums[i]≤105
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[-4,-2,1,4,8]}
Start with best = nums[0] = -4. Its distance to zero is 4, so anything closer must land strictly inside (-4, 4).