Using Map + BinarySearch (Bisect) - Python/Java/C++ 
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        n = len(nums)
        pos = {}
        for i, val in enumerate(nums):
            if val not in pos:
                pos[val] = []
            pos[val].append(i)
        
        res = []
        for q in queries:
            v = nums[q]
            ind = pos[v]
            if len(ind) == 1:
                res.append(-1)
                continue
            
            posi = bisect.bisect_left(ind, q)
            
            left = ind[posi - 1] if posi - 1 >= 0 else ind[-1]
            right = ind[posi + 1] if posi + 1 < len(ind) else ind[0]
            
            dl = abs(q - left)
            dl = min(dl, n - dl)
            dr = abs(q - right)
            dr = min(dr, n - dr)
            
            res.append(min(dl, dr))
        
        return res
```
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size();
        unordered_map<int, vector<int>> pos;
        for (int i = 0; i < n; i++) {
            pos[nums[i]].push_back(i);
        }
        vector<int> res;
        for (int q : queries) {
            int v = nums[q];
            vector<int>& ind = pos[v];
            if (ind.size() == 1) {
                res.push_back(-1);
                continue;
            }
            int posi = lower_bound(ind.begin(), ind.end(), q) - ind.begin();
            int left = (posi - 1 >= 0) ? ind[posi - 1] : ind.back();
            int right = (posi + 1 < ind.size()) ? ind[posi + 1] : ind.front();
            int dl = abs(q - left);
            dl = min(dl, n - dl);
            int dr = abs(q - right);
            dr = min(dr, n - dr);
            res.push_back(min(dl, dr));
        }
        return res;
    }
};
```
```java []
public class Solution {
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        int n = nums.length;
        Map<Integer, ArrayList<Integer>> pos = new HashMap<>();
        for (int i = 0; i < n; i++) {
            pos.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
        }
        
        List<Integer> res = new ArrayList<>();
        for (int q : queries) {
            int v = nums[q];
            ArrayList<Integer> ind = pos.get(v);
            if (ind.size() == 1) {
                res.add(-1);
                continue;
            }
            int posi = Collections.binarySearch(ind, q);
            int left = (posi - 1 >= 0) ? ind.get(posi - 1) : ind.get(ind.size() - 1);
            int right = (posi + 1 < ind.size()) ? ind.get(posi + 1) : ind.get(0);
            int dl = Math.abs(q - left);
            dl = Math.min(dl, n - dl);
            int dr = Math.abs(q - right);
            dr = Math.min(dr, n - dr);
            res.add(Math.min(dl, dr));
        }
        return res;
    }
}
```

['C++', 'Java', 'Python3']