Minimum Absolute Distance Between Mirror Pairs
You are given an integer array nums.
A pair of indices (i, j) is considered a mirror pair if:
- 0 <= i < j < nums.length
- reverse(nums[i]) == nums[j]
The reverse(x) function reverses the digits of the integer x. Any leading zeros produced during the reversal are dropped (e.g., reverse(120) = 21).
Return the minimum absolute distance abs(i - j) between the indices of any mirror pair. If no mirror pair exists, return -1.
Example 1:
Input: nums = [14, 500, 41, 5, 41]
Output: 2
Explanation:
The mirror pairs are: (0, 2): reverse(14) = 41 == nums[2].
Distance: 2 - 0 = 2. (0, 4): reverse(14) = 41 == nums[4].
Distance: 4 - 0 = 4. (1, 3): reverse(500) = 5 == nums[3]. Distance: 3 - 1 = 2. The minimum distance is 2.
Example 2:
Input: nums = [13, 24, 35]
Output: -1
Explanation:
reverse(13) = 31, reverse(24) = 42, and reverse(35) = 53. None of these reversed values match any subsequent elements in the array. Since no mirror pairs exist, return -1.
Now Your Turn!
Pick the correct output for the given inputInput: nums = [10, 1, 10]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 105
- 1 <= nums[i] <= 109