C++ O(NLogN) Solution
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 {
    int fun(int el,vector<int>&v){
        int l=0,h=(int)v.size()-1;
        while(l<=h){
            int m=(l+h)/2;
            if(v[m]<el){
                l=m+1;
            }
            else{
                h=m-1;
            }
        }
        return l;
    }
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& q) {
        int n=nums.size(),m=q.size();
        map<int,vector<int>> mp;
        for(int i=0;i<n;++i){
            mp[nums[i]].push_back(i);
        }
        vector<int> ans(m,-1);
        for(int i=0;i<m;++i){
            int ind=q[i];
            int val=nums[ind];
            if(mp[val].size()!=1){
                int x=fun(ind,mp[val]);
                int sz=mp[val].size();
                int a=(x+1);
                if(a>=mp[val].size()){
                    a=n-mp[val][x]+mp[val][0];
                }
                else{
                    a=mp[val][a];
                    a=abs(mp[val][x]-a);
                }
                int b=(x-1);
                if(b>=0){
                    b=mp[val][b];
                    b=abs(mp[val][x]-b);
                }
                else{
                    b=mp[val][x]+n-mp[val].back();
                }
                x=mp[val][x];
                ans[i]=min(a,b);
            }
        }
        return ans;
    }
};
```
['Array', 'Hash Table', 'Binary Search', 'C++']