hash map and binary search
Intuition
Iterate nums to hash map each value n to its indices in nums.
On each query J = queries[j] on n = nums[J], binary search hash map to find index i (pre
# Intuition
 - Iterate `nums` to hash map each value `n` to its `indices` in `nums`.
 - On each query `J = queries[j]` on `n = nums[J]`, binary search hash map to find index `i` (predecessor of `j`), and index `k` (successor of `j`), with circular handling, and add the smaller of distances of `j-i` and `k-j` to answer.

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        unordered_map<int, vector<int>> num2indices;
        for(int i = 0; i < nums.size(); ++i) {
            num2indices[nums[i]].push_back(i);
        }
        vector<int> ans(queries.size(), -1);
        for(int i, J = 0, k; J < queries.size(); ++J) {
            int j = queries[J];
            if(int n = nums[j]; num2indices.contains(n)) {
                auto const& indies = num2indices[n];
                //copy(indies.begin(), indies.end(), ostream_iterator<int>(cout, ",")); cout << endl;
                if(auto sz = indies.size(); sz > 1) {
                    auto b = lower_bound(indies.begin(), indies.end(), j);
                    auto a = b;
                    if (a == indies.begin()) {
                        a = indies.begin() + sz-1;
                        i = *a - nums.size();
                    }
                    else {
                        i = *--a;
                    }
                    auto c = b;
                    if (++c == indies.end()) {
                        c = indies.begin();
                        k = *c + nums.size();
                    }
                    else
                        k = *c;
                    ans[J] = min(j-i, k-j);    
                }
            }
        }
        return ans;
    }
};
```
['C++']