All problems
0518MediumArrayHash TableMath

Closest Mirror Tag

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3761Minimum Absolute Distance Between Mirror Pairs

This 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 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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def closest_mirror_tag(codes: list[int]) -> int:
Java
public int closestMirrorTag(int[] codes)
September 7
Apply