Beats 100% HashMap + Binary Search  Intuition + Approach +Code
IntuitionWe want to quickly find the closest occurrence of the same number for a given query index. To do this, we first store where each number appears. Then,
![sa.png](https://assets.leetcode.com/users/images/9054f162-8bfb-4afa-a42a-7abec4781db6_1742111899.049484.png)



# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We want to quickly find the closest occurrence of the same number for a given query index. To do this, we first store where each number appears. Then, for each query, we can look up the list of positions for that number and use binary search to find where the query index is in that list. From there, we check the neighboring positions to find the smallest distance.

# Approach
<!-- Describe your approach to solving the problem. -->
1) Preprocessing: Create a dictionary that maps each number to all its indices in the array.

2) Answering Queries:For each query index: 
If the number at that index appears only once, return -1.
Otherwise, find the index of the query in the list of positions using binary search.
Check the distances to the previous and next occurrences (if available) and also consider a circular distance (wrapping from the end of the array back to the start).
Choose the smallest distance and add it to the result.

# Complexity
- Time complexity: O(n+mlog(n))
<!-- Add you time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(n+m)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        from collections import defaultdict
        res=[]
        has=defaultdict(list)
        for i in range(len(nums)):
            has[nums[i]].append(i)
        for each in queries:
            if len(has[nums[each]])>1:
                low=0
                high=len(has[nums[each]])
                while low<=high:
                    mid=(low+high)//2
                    if has[nums[each]][mid]==each:
                        found=mid
                        break
                    elif has[nums[each]][mid]<each:
                        low=mid+1
                    else:
                        high=mid-1
                if found==0:
                    circular=has[nums[each]][found]+1+len(nums)-(has[nums[each]][-1])-1
                    mini=min(abs(has[nums[each]][found+1]-has[nums[each]][found]),circular)
                    res.append(mini)
                elif found==len(has[nums[each]])-1:
                    circular=(len(nums)-has[nums[each]][found]+1)+(len(nums))-(len(nums)-(has[nums[each]][0])+1)
                    mini=min(abs(has[nums[each]][found-1]-has[nums[each]][found]),circular)
                    res.append(mini)
                else:
                    mini=min(abs(has[nums[each]][found+1]-has[nums[each]][found]),abs(has[nums[each]][found-1]-has[nums[each]][found]))
                    res.append(mini)
            else:
                res.append(-1)
        return res


```
['Python3']