Skip to main content

Minimum Absolute Distance Between Mirror Pairs - Solution & Explanation

MediumArrayHash TableMath9 min readAsked at: Amazon, Microsoft, Meta +1
Practice this problem

Problem Statement

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.

 

Example 1:

Input: nums = [12,21,45,33,54]

Output: 1

Explanation:

The mirror pairs are:

  • (0, 1) since reverse(nums[0]) = reverse(12) = 21 = nums[1], giving an absolute distance abs(0 - 1) = 1.
  • (2, 4) since 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 <= 105
  • 1 <= nums[i] <= 109​​​​​​​

Approach Overview

Problem Overview: Given an array of integers, you need the smallest absolute distance between indices i and j such that the two values form a mirror pair. A mirror pair means one value is the mathematical mirror of the other (for example x and -x). The goal is to scan the array and return the minimum |i - j| among all valid pairs.

Approach 1: Brute Force Pair Check (O(n²) time, O(1) space)

The straightforward method checks every pair of indices. For each i, iterate through all j > i and test whether nums[i] and nums[j] are mirrors. When a mirror condition is satisfied, compute the absolute index difference and track the minimum. This approach is easy to implement but inefficient for large arrays because it performs roughly n × n comparisons. It mainly serves as a baseline and demonstrates the problem constraints clearly.

Approach 2: Hash Table Lookup (O(n) time, O(n) space)

The optimal solution uses a hash table to remember previously seen numbers and their indices while scanning the array once. When processing an element x at index i, compute its mirror value -x. A constant-time hash lookup tells you whether that mirror appeared earlier. If it exists, compute the index distance |i - prevIndex| and update the minimum distance.

This approach works because each element’s mirror relationship can be checked instantly with a hash map instead of searching the entire array. You simply iterate through the array, perform a hash lookup, and update the stored index for the current number. The algorithm runs in linear time with additional storage for the map. The mirror relationship relies on a simple math transformation (-x), which keeps the logic straightforward.

Recommended for interviews: Start by mentioning the brute force pair comparison to show you understand the definition of mirror pairs and the distance requirement. Then move quickly to the hash table approach. Interviewers expect the O(n) single-pass solution because it demonstrates familiarity with hash-based lookups and common array optimization patterns.

Solution

We can use a hash table pos to record the last occurrence position of each reversed number.

We first initialize the answer ans = n + 1, where n is the length of the array nums.

Next, we iterate through the array nums. For each index i and its corresponding number x = nums[i], if the key x exists in pos, it means there exists an index j such that nums[j] reversed equals x. In this case, we update the answer to min(ans, i - pos[x]). Then, we update pos[reverse(x)] to i. Continue this process until we finish iterating through the entire array.

Finally, if the answer ans is still equal to n + 1, it means no mirror pair exists, and we return -1; otherwise, we return the answer ans.

The time complexity is O(n times log M), where n is the length of the array nums, and M is the maximum value in the array. The space complexity is O(n), which is used to store the hash table pos.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pair CheckO(n²)O(1)Useful for understanding the mirror condition or when the array size is very small
Hash Table LookupO(n)O(n)General case and interview-preferred solution for large arrays

Video Solution

Minimum Absolute Distance Between Mirror Pairs | Simplest Explanation | Leetcode 3761 | MIK • codestorywithMIK • 4,251 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Absolute Distance Between Mirror Pairs easy or hard?
The problem is generally classified as Medium. The mirror condition is simple, but recognizing that a hash table reduces the brute force O(n^2) search to an O(n) single pass requires familiarity with array optimization techniques.
Minimum Absolute Distance Between Mirror Pairs Python/Java solution
The typical implementation uses a hash map or dictionary. In Python, use a dict to map numbers to indices. In Java, use a HashMap<Integer, Integer>. Each iteration checks if the mirror value exists and updates the minimum index distance.
How to solve Minimum Absolute Distance Between Mirror Pairs in O(n)?
Traverse the array while storing values in a hash map with their indices. For each element x at index i, compute the mirror value -x and check if it already exists in the map. If found, calculate |i - previousIndex| and update the minimum distance. Continue until the array ends.
What is the best approach for Minimum Absolute Distance Between Mirror Pairs?
The hash table approach is the most efficient. Iterate through the array once while storing each number and its index in a hash map. For each value x, check whether its mirror value -x already exists in the map. This gives an O(n) time solution with O(n) extra space.
Is Minimum Absolute Distance Between Mirror Pairs asked at Google/Amazon/Meta?
Problems involving hash maps, array scanning, and pair relationships frequently appear in interviews at companies like Amazon, Google, and Meta. Variants of mirror or complement pair problems are common because they test hash table optimization patterns.
What data structure is used in Minimum Absolute Distance Between Mirror Pairs?
A hash table (hash map) is the key data structure. It stores numbers and their indices so you can check for mirror values in constant time while scanning the array.
What is the time complexity of Minimum Absolute Distance Between Mirror Pairs?
The optimal hash table solution runs in O(n) time because each element is processed once and hash lookups are O(1) on average. The brute force approach takes O(n^2) time since it compares every pair of elements in the array.

Ready to solve this problem?

Practice Minimum Absolute Distance Between Mirror Pairs with our built-in code editor and test cases.

Practice on FleetCode