Easy solution
Intuition:We need to find the minimum distance (on a circular array) from the queried index to any other index having the same value. The key idea is to precomp
# Intuition:
We need to find the minimum distance (on a circular array) from the queried index to any other index having the same value. The key idea is to precompute the indices for every unique value in the array. This allows us to quickly access and search for the closest occurrence of a value when processing each query.

# Approach:
1. Traverse the array and build an unordered_map that maps each value to a vector of indices where it occurs.
2. Sort the vector of indices for each value so that binary search can be used efficiently.
3. For each query:
   - Retrieve the value at the queried index.
   - If the value occurs only once, return -1.
   - Otherwise, use binary search (`lower_bound`) on the sorted vector of indices to locate the position of the queried index.
   - Check the neighbor to the right and left (taking care to wrap around in the circular array).
   - Compute the circular distance by taking the minimum between the direct distance and the wrap-around distance.
   - Append the smallest distance to the answer.

# Complexity:
- Time Complexity: O(n + q · log k)
  * O(n) to build the map.
  * O(q · log k) for binary search in each query (where k is the frequency of the queried element).
- Space Complexity: O(n)
  * To store the mapping from values to indices.
# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& arr, vector<int>& que) {
        int n = arr.size();
        unordered_map<int, vector<int>> mp;
        for (int i = 0; i < n; i++)
            mp[arr[i]].push_back(i);
        for (auto & pr : mp)
            sort(pr.second.begin(), pr.second.end());
        vector<int> ans;
        for (int x : que) {
            int v = arr[x];
            vector<int>& pos = mp[v];
            if (pos.size() == 1) {
                ans.push_back(-1);
            } else {
                auto it = lower_bound(pos.begin(), pos.end(), x);
                int idx = it - pos.begin();
                
                int c1 = (idx == pos.size() - 1) ? pos[0] : pos[idx + 1];
                int d1 = abs(x - c1);
                d1 = min(d1, n - d1);
        
                int c2 = (idx == 0) ? pos.back() : pos[idx - 1];
                int d2 = abs(x - c2);
                d2 = min(d2, n - d2);
            
                ans.push_back(min(d1,d2));
            }
        }
        return ans;
    }
};

```
['C++']