Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums.
A mirror pair is a pair of indices (i, j) such that:
0 <= i < j < nums.length, andreverse(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:
Input: nums = [12,21,45,33,54]
Output: 1
Explanation:
The mirror pairs are:
reverse(nums[0]) = reverse(12) = 21 = nums[1], giving an absolute distance abs(0 - 1) = 1.reverse(nums[2]) = reverse(45) = 54 = nums[4], giving an absolute distance abs(2 - 4) = 2.The minimum absolute distance among all pairs is 1.
Example 2:
Input: nums = [120,21]
Output: 1
Explanation:
There is only one mirror pair (0, 1) since reverse(nums[0]) = reverse(120) = 21 = nums[1].
The minimum absolute distance is 1.
Example 3:
Input: nums = [21,120]
Output: -1
Explanation:
There are no mirror pairs in the array.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109Problem summary: You are given an integer array nums. A mirror pair is a pair of indices (i, j) such that: 0 <= i < j < nums.length, and 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[12,21,45,33,54]
[120,21]
[21,120]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3761: Minimum Absolute Distance Between Mirror Pairs
class Solution {
public int minMirrorPairDistance(int[] nums) {
int n = nums.length;
Map<Integer, Integer> pos = new HashMap<>(n);
int ans = n + 1;
for (int i = 0; i < n; ++i) {
if (pos.containsKey(nums[i])) {
ans = Math.min(ans, i - pos.get(nums[i]));
}
pos.put(reverse(nums[i]), i);
}
return ans > n ? -1 : ans;
}
private int reverse(int x) {
int y = 0;
for (; x > 0; x /= 10) {
y = y * 10 + x % 10;
}
return y;
}
}
// Accepted solution for LeetCode #3761: Minimum Absolute Distance Between Mirror Pairs
func minMirrorPairDistance(nums []int) int {
n := len(nums)
pos := map[int]int{}
ans := n + 1
reverse := func(x int) int {
y := 0
for ; x > 0; x /= 10 {
y = y*10 + x%10
}
return y
}
for i, x := range nums {
if j, ok := pos[x]; ok {
ans = min(ans, i-j)
}
pos[reverse(x)] = i
}
if ans > n {
return -1
}
return ans
}
# Accepted solution for LeetCode #3761: Minimum Absolute Distance Between Mirror Pairs
class Solution:
def minMirrorPairDistance(self, nums: List[int]) -> int:
def reverse(x: int) -> int:
y = 0
while x:
v = x % 10
y = y * 10 + v
x //= 10
return y
pos = {}
ans = inf
for i, x in enumerate(nums):
if x in pos:
ans = min(ans, i - pos[x])
pos[reverse(x)] = i
return -1 if ans == inf else ans
// Accepted solution for LeetCode #3761: Minimum Absolute Distance Between Mirror Pairs
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3761: Minimum Absolute Distance Between Mirror Pairs
// class Solution {
// public int minMirrorPairDistance(int[] nums) {
// int n = nums.length;
// Map<Integer, Integer> pos = new HashMap<>(n);
// int ans = n + 1;
// for (int i = 0; i < n; ++i) {
// if (pos.containsKey(nums[i])) {
// ans = Math.min(ans, i - pos.get(nums[i]));
// }
// pos.put(reverse(nums[i]), i);
// }
// return ans > n ? -1 : ans;
// }
//
// private int reverse(int x) {
// int y = 0;
// for (; x > 0; x /= 10) {
// y = y * 10 + x % 10;
// }
// return y;
// }
// }
// Accepted solution for LeetCode #3761: Minimum Absolute Distance Between Mirror Pairs
function minMirrorPairDistance(nums: number[]): number {
const n = nums.length;
const pos = new Map<number, number>();
let ans = n + 1;
const reverse = (x: number) => {
let y = 0;
for (; x > 0; x = Math.floor(x / 10)) {
y = y * 10 + (x % 10);
}
return y;
};
for (let i = 0; i < n; ++i) {
if (pos.has(nums[i])) {
const j = pos.get(nums[i])!;
ans = Math.min(ans, i - j);
}
pos.set(reverse(nums[i]), i);
}
return ans > n ? -1 : ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.