hash map + binary search
Intuitionhash_map + binary searchApproachComplexity
Time complexity:
O(n) + O(|q| * log(n))
Space complexity:
O(n)Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
hash_map + binary search
# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n) + O(|q| * log(n))
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(n)

# Code
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        hash_map = defaultdict(list)
        result = []
        N = len(nums)
        for i,x in enumerate(nums):
            hash_map[x].append(i)
        for q in queries:
            q_num = nums[q]
            temp = hash_map[q_num]
            idx_in_temp = bisect.bisect_left(temp,q)
            if(len(temp)==1):
                result.append(-1)
            elif(idx_in_temp == 0):
                result.append(min(temp[-1] - temp[0],temp[1]-temp[0],temp[0] +1 + N-1-temp[-1]))
            elif(idx_in_temp == len(temp) -1):
                result.append(min(temp[-1] - temp[-2],temp[-1] - temp[0],N-1 - temp[-1] + temp[0] + 1))
            else:
                result.append(min(temp[idx_in_temp] - temp[idx_in_temp -1],temp[idx_in_temp +1] - temp[idx_in_temp],))
        return result




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