Trains the technique from
LeetCode 219Contains Duplicate IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A lathe operator keeps a trim log. trims[i] is the signed correction, in micrometres, that was dialled in before pass i: a negative figure pulls the cut back, a positive figure pushes it forward, and 0 means the pass ran with the dial untouched.
Quality control cares about corrections that come back around quickly, because a value reused soon after its last use points at a machine that is drifting rather than settling. You are given the log and an integer span.
Report whether some correction figure was dialled in on two separate passes whose pass numbers are no more than span apart. Return true when such a pair of passes exists and false when none does. A pass is never a repeat of itself, so a span of 0 can only ever give false.
Example 1
The correction -2 was dialled in before pass 1 and again before pass 3, and those passes sit 2 apart, which the span allows.
Example 2
Only the figure 5 was reused, on passes 0 and 2. Those passes are 2 apart, so with a span of 1 the log counts as settled.
Example 3
The reused figure 8 sits on the first and last passes, exactly 3 apart, so the final pass still has to be weighed against the whole span behind it.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def has_nearby_repeat(trims: list[int], span: int) -> bool:public boolean hasNearbyRepeat(int[] trims, int span)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.