🔥🚀Simple Clean code using Binary Search and Map  💡
Complexity

Time complexity:
O(n+qlogn)


Space complexity:
O(n)

Code
# Complexity
- Time complexity:
$$O(n+qlogn)$$

- Space complexity:
$$O(n)$$

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& arr, vector<int>& queries) 
    {
        int n=arr.size();
        vector<int> ans(queries.size());
        map<int,vector<int>> freq;
        for(int i=0;i<n;i++) freq[arr[i]].push_back(i);
        for(int i=0;i<queries.size();i++)
        {
            int curr=arr[queries[i]];
            if(freq[curr].size()<2) ans[i]=-1;
            else 
            {
                int index=lower_bound(freq[curr].begin(),freq[curr].end(),queries[i])-freq[curr].begin();
                int m=freq[curr].size();
                int prev_index = (index - 1 + m) % m;
                int next_index = (index + 1) % m;
                ans[i] = min((queries[i] - freq[curr][prev_index] + n) % n, (freq[curr][next_index] - queries[i] + n) % n);
            }
        }
        return ans;
    }
};
```
['Hash Table', 'Binary Search', 'C++']