hashmap + sorting
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
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size(), m = queries.size();
        
        vector<int> ans(m, -1);
        unordered_map<int, vector<int>> num_indices;
        
        for(int i = 0; i < n; ++i) 
            num_indices[nums[i]].push_back(i);
        
        for(auto& [num, vec] : num_indices)
            sort(begin(vec), end(vec));
        
        for(int i = 0; i < m; ++i) {
            int idx = queries[i];
                
            if(idx >= n)
                continue;
            
            int num = nums[idx];
            
            if(num_indices[num].size() <= 1)
                continue;
            
            auto& vec = num_indices[num];
            int pos = lower_bound(begin(vec), end(vec), idx) - begin(vec);
            
            int left_idx = vec[(pos - 1 + vec.size()) % vec.size()];
            int right_idx = vec[(pos + 1) % vec.size()];
            
            int dist = (idx - left_idx + n) % n;
            dist = min(dist, (right_idx - idx + n) % n);

            ans[i] = dist;
        }
        
        return ans;
    }
};
```
['Hash Table', 'Sorting', 'C++']