Ctrl K

Nearest to Zero

Easy
A thermometer logged a few readings, readings: integers that may be negative. Find the reading closest to zero and return it. If several readings are equally close (say, -3 and 3), return the largest of them.
Example 1
readings=[-6, 3, -2, 9] → -2

-2 is closest to zero, at distance 2; 3 is at distance 3.

Example 2
readings=[-5, 5, 8] → 5

-5 and 5 are equally far from zero, so the larger one, 5, wins.

Example 3
readings=[0, -1] → 0
Constraints
  • 1 ≤ len(readings) ≤ 1000
  • -10⁵ ≤ readings[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
readings=[-6, 3, -2, 9]
Expected
-2

Run checks the first 3 tests in your browser. Submit checks all 130 tests on the server, hidden ones included.