Introduction
Some problems look simple at first—but hide a clever trick inside.
LeetCode 3761 – Minimum Absolute Distance Between Mirror Pairs is one such problem. It combines:
- Number manipulation (digit reversal)
- Hashing
- Efficient searching
If approached naively, this problem can easily lead to O(n²) time complexity—which is not feasible for large inputs.
In this article, we will walk through:
- Problem intuition
- Naive approach (and why it fails)
- Optimized HashMap solution
- Step-by-step explanation
- Clean Java code with comments
🔗 Problem Link
LeetCode: Minimum Absolute Distance Between Mirror Pairs
To gain a deeper understanding of the problem, it is highly recommended that you review this similar problem Closest Equal Element Queries here is the link of the article. Both cases follow a nearly identical pattern, and studying the initial example will provide valuable context for the current task.
Problem Statement
You are given an integer array nums.
A mirror pair (i, j) satisfies:
0 ≤ i < j < nums.lengthreverse(nums[i]) == nums[j]
👉 Your task is to find:
The minimum absolute distance between such pairs
If no mirror pair exists, return -1.
Examples
Example 1
Input:
nums = [12, 21, 45, 33, 54]
Output:
1
Explanation:
- (0,1) → reverse(12) = 21 → distance = 1
- (2,4) → reverse(45) = 54 → distance = 2
- ✔ Minimum = 1
Example 2
Input:
nums = [120, 21]
Output:
1
Example 3
Input:
nums = [21, 120]
Output:
-1
Key Insight
The core idea is:
❌ Naive Approach (Brute Force)
Idea
- Check all pairs
(i, j) - Reverse nums[i]
- Compare with nums[j]
Complexity
- Time: O(n²) ❌
- Space: O(1)
Problem
With n ≤ 100000, this approach will definitely cause TLE.
✅ Optimized Approach (HashMap)
Intuition
While iterating through the array:
- Reverse the current number
- Check if this number was already seen as a reversed value
- If yes → we found a mirror pair
Key Trick
Instead of storing original numbers:
👉 Store reversed values as keys
This allows instant lookup.
Java Code (With Detailed Comments)
Step-by-Step Dry Run
Input:
Execution:
| Index | Value | Reverse | Map Check | Action |
| 0 | 12 | 21 | not found | store (21 → 0) |
| 1 | 21 | 12 | found | distance = 1 |
| 2 | 45 | 54 | not found | store (54 → 2) |
| 3 | 33 | 33 | not found | store (33 → 3) |
| 4 | 54 | 45 | found | distance = 2 |
👉 Minimum = 1
Complexity Analysis
Time Complexity
- Reversing number → O(digits) ≈ O(log n)
- Loop → O(n)
👉 Overall: O(n)
Space Complexity
- HashMap stores at most
nelements
👉 O(n)
Why This Approach Works
- Avoids unnecessary pair comparisons
- Uses hashing for constant-time lookup
- Processes array in a single pass
Key Takeaways
- Always think of hashing when matching conditions exist
- Reversing numbers can convert the problem into a lookup problem
- Avoid brute force when constraints are large
- This is a classic “store & check” pattern
Common Interview Pattern
This problem is similar to:
- Two Sum (hashing)
- Reverse pairs
- Matching transformations
Conclusion
The Minimum Absolute Distance Between Mirror Pairs problem is a great example of how a simple optimization (using a HashMap) can reduce complexity from O(n²) → O(n).
Understanding this pattern will help you solve many similar problems involving:
- Transformations
- Matching conditions
- Efficient lookups
Frequently Asked Questions (FAQs)
1. Why store reversed value instead of original?
Because we want to quickly check if a number matches the reverse of a previous number.
2. What if multiple same reversed values exist?
The map stores the latest index, ensuring minimum distance is considered.
3. Can this be solved without HashMap?
Yes, but it will result in inefficient O(n²) time.




