Hash Table, Binary Search, and some Mod
Intuitiondetermine the minimum circular distance between the element at a given index and any other index with the same value in a circular array nums.ApproachP
# Intuition
determine the minimum circular distance between the element at a given index and any other index with the same value in a circular array nums.

# Approach
Preprocessing via a hashmap (`defaultdict`) to accelerate further binary search. Here we constructed it along index so each list is sorted.
Circular means mod: We can mod on index of positionlist and also mod on raw position list.



# Complexity
- Time complexity:
$\mathcal{O}(n+m\log n)$
where $n$ is length of `nums` and `m` is length of `queries`.
worst case $\mathcal{O}(n^2+m\log n)$ for poor hash.
- Space complexity:
$\mathcal{O}(m+n)$

# Code
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        n = len(nums)
        dct_num_pos = defaultdict(list)
        for i, num in enumerate(nums):
            # notice it's sorted already
            dct_num_pos[num].append(i)
        res = []
        for q in queries:
            lst_pos = dct_num_pos[nums[q]]
            m = len(lst_pos)
            if m <= 1:
                # if occurred once or less
                res.append(-1)
            else:
                # now we know it can be found
                idx_pos = bisect.bisect_left(lst_pos, q)
                res.append(
                    min(
                        (q - lst_pos[(idx_pos - 1) % m]) % n,
                        (lst_pos[(idx_pos + 1) % m] - q) % n,
                    )
                )
        return res

```
['Hash Table', 'Math', 'Binary Search', 'Python3']