Trains the technique from
LeetCode 34Find First and Last Position of Element in Sorted ArrayThis 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 machine shop keeps an archive of how far finished parts drifted from a reference line, measured in tenths of a millimetre. A part on the long side of the line records a positive drift and a part on the short side records a negative one.
The archive readings is filed from smallest drift to largest, never decreasing as you move forward. Any drift may be filed more than once, because different parts can miss the line by the very same amount. Because the filing is ordered, all copies of one drift sit shoulder to shoulder in a single unbroken run.
Given the archive and one drift value, report where that run begins and where it ends: return [first, last], the lowest index holding value and the highest index holding value. When the archive holds no copy of value at all, return [-1, -1] instead.
The archive can grow to a hundred thousand readings and may also be completely empty. Each answer has to be produced in time that grows only with the logarithm of the archive size, so stepping through the readings one after another is not fast enough.
Example 1
Three parts drifted -1 tenths and they occupy indices 1, 2 and 3. Index 0 holds a smaller drift and index 4 already holds a larger one.
Example 2
A drift of 5 would belong between 3 and the end of the archive, but no part actually recorded it, so the absent marker is returned.
Example 3
Every filed part drifted the same amount, so the run stretches from the first index to the last.
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 value_span(readings: list[int], value: int) -> list[int]:public int[] valueSpan(int[] readings, int value)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.