Trains the technique from
LeetCode 3872Longest Arithmetic Sequence After Changing At Most One ElementThis 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 survey crew has driven markers along a rail line and written down readings, where readings[i] is the distance recorded at marker i, in order along the line.
A stretch is a block of markers at consecutive positions of readings. A stretch is evenly spaced when the difference between each pair of neighbouring readings inside it is the same throughout. A stretch of one or two markers is evenly spaced, since it has no pair of differences to disagree.
Before measuring, the crew may re-cut at most one marker: choose at most one index of readings and replace its reading with any integer you like, which may be negative and may repeat another reading. Only one index may be replaced in total.
Return the number of markers in the longest evenly spaced stretch that exists after the re-cut.
Example 1
Re-cut marker 3 to 12. The readings become 3, 6, 9, 12, 15, 18, where every neighbouring pair differs by 3, so all six markers sit in one evenly spaced stretch.
Example 2
Re-cut marker 2 to 9. Markers 0, 1 and 2 then read 1, 5, 9, a stretch of three whose neighbouring readings differ by 4 throughout.
Example 3
Re-cut marker 0 to -2. The readings become -2, 1, 4, 7, 10, spaced 3 apart the whole way, so the stretch covers all five markers. A replacement reading is allowed to be negative.
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 longest_even_stretch(readings: list[int]) -> int:public int longestEvenStretch(int[] readings)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.