|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-absolute-distance-between-mirror-pairs">Minimum Absolute Distance Between Mirror Pairs</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>You are given an integer array <code>nums</code>.</p> |
| 2 | + |
| 3 | +<p>A <strong>mirror pair</strong> is a pair of indices <code>(i, j)</code> such that:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>0 <= i < j < nums.length</code>, and</li> |
| 7 | + <li><code>reverse(nums[i]) == nums[j]</code>, where <code>reverse(x)</code> denotes the integer formed by reversing the digits of <code>x</code>. Leading zeros are omitted after reversing, for example <code>reverse(120) = 21</code>.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>Return the <strong>minimum</strong> absolute distance between the indices of any mirror pair. The absolute distance between indices <code>i</code> and <code>j</code> is <code>abs(i - j)</code>.</p> |
| 11 | + |
| 12 | +<p>If no mirror pair exists, return <code>-1</code>.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<div class="example-block"> |
| 18 | +<p><strong>Input:</strong> <span class="example-io">nums = [12,21,45,33,54]</span></p> |
| 19 | + |
| 20 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 21 | + |
| 22 | +<p><strong>Explanation:</strong></p> |
| 23 | + |
| 24 | +<p>The mirror pairs are:</p> |
| 25 | + |
| 26 | +<ul> |
| 27 | + <li>(0, 1) since <code>reverse(nums[0]) = reverse(12) = 21 = nums[1]</code>, giving an absolute distance <code>abs(0 - 1) = 1</code>.</li> |
| 28 | + <li>(2, 4) since <code>reverse(nums[2]) = reverse(45) = 54 = nums[4]</code>, giving an absolute distance <code>abs(2 - 4) = 2</code>.</li> |
| 29 | +</ul> |
| 30 | + |
| 31 | +<p>The minimum absolute distance among all pairs is 1.</p> |
| 32 | +</div> |
| 33 | + |
| 34 | +<p><strong class="example">Example 2:</strong></p> |
| 35 | + |
| 36 | +<div class="example-block"> |
| 37 | +<p><strong>Input:</strong> <span class="example-io">nums = [120,21]</span></p> |
| 38 | + |
| 39 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 40 | + |
| 41 | +<p><strong>Explanation:</strong></p> |
| 42 | + |
| 43 | +<p>There is only one mirror pair (0, 1) since <code>reverse(nums[0]) = reverse(120) = 21 = nums[1]</code>.</p> |
| 44 | + |
| 45 | +<p>The minimum absolute distance is 1.</p> |
| 46 | +</div> |
| 47 | + |
| 48 | +<p><strong class="example">Example 3:</strong></p> |
| 49 | + |
| 50 | +<div class="example-block"> |
| 51 | +<p><strong>Input:</strong> <span class="example-io">nums = [21,120]</span></p> |
| 52 | + |
| 53 | +<p><strong>Output:</strong> <span class="example-io">-1</span></p> |
| 54 | + |
| 55 | +<p><strong>Explanation:</strong></p> |
| 56 | + |
| 57 | +<p>There are no mirror pairs in the array.</p> |
| 58 | +</div> |
| 59 | + |
| 60 | +<p> </p> |
| 61 | +<p><strong>Constraints:</strong></p> |
| 62 | + |
| 63 | +<ul> |
| 64 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 65 | + <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> |
| 66 | +</ul> |
0 commit comments