Minimum Absolute Distance Between Mirror Pairs

You are given an integer array nums.

A mirror pair is a pair of indices (i, j) such that:

  • 0 <= i < j < nums.length
  • reverse(nums[i]) == nums[j], where reverse(x) denotes the integer formed by reversing the digits of x; leading zeros are omitted after reversing, for example reverse(120) = 21.

Return the minimum absolute distance between the indices of any mirror pair. The absolute distance between indices i and j is abs(i - j).

If no mirror pair exists, return -1.

Example 1
Inputnums = [12,21,45,33,54]
Output1
The mirror pairs are (0, 1) with distance 1 and (2, 4) with distance 2, so the minimum absolute distance is 1.
Example 2
Inputnums = [120,21]
Output1
There is only one mirror pair (0, 1) since reverse(nums[0]) = reverse(120) = 21 = nums[1], so the minimum absolute distance is 1.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate