Simple @Hash & Binary Search
Complexity
Time complexity:
O(n log n)
Space complexity:
O(n)Code

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n log n)
---
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(n)
---
# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& qr) {
        int n = nums.size(), m = qr.size(), mxSize = *max_element(nums.begin(), nums.end()) + 1;
        vector<int> ans(m, -1);
        unordered_map<int, vector<int>> mp;
        for (int i = 0; i < n; i++) 
            mp[nums[i]].push_back(i);

        for (int i = 0; i < m; i++) {
            int val = nums[qr[i]];
            if (mp[val].size() == 1) continue;
            int idx = lower_bound(mp[val].begin(), mp[val].end(), qr[i]) - mp[val].begin();
            int last = mp[val].size() - 1;
            int preDist = (idx == 0) ? mp[val][0] + n - mp[val][last] : mp[val][idx] - mp[val][idx - 1];
            int postDist = (idx == last) ? n - mp[val][idx] + mp[val][0] : mp[val][idx + 1] - mp[val][idx];
            ans[i] = min(preDist, postDist);
        }
        return ans;
    }
};
```
['Array', 'Hash Table', 'Binary Search', 'C++']