LeetCode 2239 • Visual Guide

Closest to Zero, visually obvious.

Watch the scan sweep, see ties flash, and remember: when absolute distances tie, the larger value wins. Everything here glows, moves, and stays accessible.

Graphics-first • Playable • Premium motion
Auto demo Loop cycles through scan → tie moment → winner snap.
Loading loop…
Problem
Tie-break spotlight
Always O(n)

Find the closest number to zero

If multiple values share the same absolute distance to 0, choose the larger value. Everything on this page exists to make that rule memorable.

Input [3, -4, 2, -1]
Winner 2
Tie-break rule Larger value wins ties
Interactive playground
Edit numbers inline, shuffle, randomize, or load presets. Step through or view instantly.
Mode
Array

Number line

Distance bars

Explain it to me
Hover or focus a chip to see why it wins or loses.
How it works
Single pass, track best value + absolute distance.
for value in nums abs(value) best? abs smaller? tie? take larger

State model

We track four things: current index, candidate value, absolute distance, and the best-so-far. In step mode, you see the cursor move and the best chip pulse.

  • Candidate beats best if |x| is smaller.
  • On a tie of |x|, choose the larger number.
  • One scan over the array: O(n) time, O(1) extra space.
Tie-break spotlight
Pick k and watch -k vs k. The larger number wins ties.

Outcome

k wins ties because it is numerically larger.

Complexity
Single linear sweep with constant memory.

Time

O(n): we visit each chip exactly once.

Space

O(1): tracking best value and its distance only.

Reliability cues

Winner locks in with a glow + crown, ties double flash, reduced motion replaces flashes with solid highlights.

Code showcase
Copy-ready, deterministic, mirrors the visuals.

      
Share / CTA
Link encodes the array + mode. Card previews your current state.