Minimum Absolute Distance Between Mirror Pairs
Real-world version: Closest Mirror TagThe statement here is our own: the same inputs, outputs and technique.
A rail holds tags in a row, and codes[i] is the positive whole number printed on the tag at position i.
The mirror of a number is what you read when its decimal digits are read back to front, with any leading zeros the reversal produces thrown away. The mirror of 47 is 74, the mirror of 1200 is 21, and the mirror of 66 is 66.
Two positions i and j with i < j form a mirror pair when at least one of the two printed numbers is the mirror of the other, and the gap of that pair is j - i.
Return the smallest gap over all mirror pairs on the rail. Every gap is at least 1, so return -1 when the rail holds no mirror pair at all.
Examples
Example 1
- Input
- codes = [13, 5, 7, 31]
- Output
- 3
Position 0 prints 13 and position 3 prints 31, which is the mirror of 13, so that pair has gap 3 and no other pair on the rail forms a mirror pair.
Example 2
- Input
- codes = [120, 8, 21, 3, 21, 120]
- Output
- 1
Position 4 prints 21 and position 5 prints 120, whose mirror is 21, so that pair has gap 1.
Example 3
- Input
- codes = [10, 20, 30]
- Output
- -1
The mirrors of 10, 20 and 30 are 1, 2 and 3, none of which is printed on the rail, so no pair qualifies.
Constraints
- 1 <= codes.length <= 10^5
- 1 <= codes[i] <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def closest_mirror_tag(codes: list[int]) -> int:public int closestMirrorTag(int[] codes)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's