Trains the technique from
LeetCode 3488Closest Equal Element QueriesThis 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 picking carousel has n storage bays bolted round a closed ring, numbered 0 to n - 1 in the order they sit on the ring, with bay n - 1 sitting right next to bay 0. Bay i holds the product coded codes[i]; the same code may be stocked in several bays.
One step of the carousel moves you from a bay to either of the two bays beside it, so it can be turned in either direction.
For every entry in asks, treat asks[j] as a starting bay and report the fewest steps from it to a different bay holding the same code. Report -1 for that entry when no other bay holds that code. Because a step count is never negative, -1 cannot be confused with a real answer.
Return the answers as a list in the same order as asks.
Example 1
Bays 0 and 4 both hold code 7 and sit next to each other across the seam of the ring. Bays 1 and 3 both hold code 2. Code 9 is stocked once only.
Example 2
Code 7 sits in bays 0, 4 and 7. From bay 0 the nearest is bay 7, from bay 7 it is bay 0, and from bay 4 it is bay 7.
Example 3
With only three bays and one code, every bay has a neighbour holding the same code one step away.
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 nearest_twin_steps(codes: list[int], asks: list[int]) -> list[int]:public List<Integer> nearestTwinSteps(int[] codes, int[] asks)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.