Using Binary Search and HashMap || Python || Beats 100% || Easy Explanation
IntuitionWe are given two integer arrays nums and queries. The array queries contains indices of the array nums. For each query in the array queries we have to
# Intuition
We are given two integer arrays `nums` and `queries`. The array `queries` contains indices of the array `nums`. For each query in the array `queries` we have to find the closest index whose value is equal to value at the current query index. In other words, if `q` is the current  query then we have to find the closest index `i` such that `nums[queries[q]] == nums[i]`. If no index is found then we have to return `-1` for that query.

One way to solve this problem is, for each query, traverse the entire array `nums` to find the closest index. But this will take `O(N^2)` time, which is not feasible.

When searching linearly is not feasible, then we will use binary search.

# Approach
We can't directly apply binary search on the `nums` array as we can't sort the array. If we sort the array, we will lose the indices. So, first we have to map elements of the array with the indices.
Initially, we will create a hashmap where key will be the element and the value will be the list of indices at which this element occurs.

Now, for each query, we retrieve the list of indices at which the current query element is present using the hashmap and we perform binary search on this list of indices. As we filled this list from left to right, this list will be sorted by default. In this list, we have to perform binary search to find the position of the current query. The indices which are on left and right of the current query will be the closest indices.
For the left index and right index, we have to calculate the distance and the minimum distance will the answer for the current query.

# Complexity
First we are filling the hashmap which will take `O(N)` time. Then we are iterating on `queries` array which is of length `q`. For each query, we are performing a binary search which will take `O(logN)` time (In worst case the length of the indices list will be N).
- Time complexity: O(N) + (qlogN)

We are storing a hashmap
- Space complexity: O(N)

# Code
```python3 []
import bisect # It is used to perform binary search

class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        n = len(nums)
        indexMap = defaultdict(list)
        for i in range(n):
            indexMap[nums[i]].append(i)

        res = []
        for q in queries:
            indices = indexMap[nums[q]]

            if len(indices) <= 1:
                res.append(-1)
                continue

            # Performing binary search to find the position of q
            ind = bisect.bisect_left(indices, q)
            m = len(indices)
            l = (ind - 1) % m
            r = (ind + 1) % m

            dist1 = min(abs(q - indices[l]), n - abs(q - indices[l]))
            dist2 = min(abs(q - indices[r]), n - abs(q - indices[r]))
            res.append(min(dist1, dist2))

        return res
```
['Array', 'Hash Table', 'Binary Search', 'Sorting', 'Python3']