Map + BS || C++ 
class Solution {
public:};
class Solution {
public:

    int BS(vector<int> &v, int i) {
        int left = 0;
        int right = v.size()-1;
        int ans = 0;
        while(left <=right) {
            int mid = left + (right-left)/2;
            if(v[mid] == i) {
                ans = mid;
                return ans;
            } else if(v[mid] > i) {
                right = mid-1;
            } else {
                left = mid+1;
            }
        }
        return ans;
    }

    int findMinDis(vector<int> &v, int i, int n) {
        int ans = INT_MAX;

        //using BS to find indexs near index i 
        int idx = BS(v, i);
        int right = idx+1;
        int left = idx-1;

        if(right < v.size() && left >= 0) {
            ans = min(v[right]-i, i-v[left]);
        } else if( right >= v.size() && left >= 0) {
            int mi = min({abs(i-v[left]), n-i+v[left], n-i+v[0]});
            ans = min(mi, ans); 
        } else if ( right < v.size() && left < 0) {
            int mi = min({abs(v[right] - i), n+i-v[right], n+i-v[v.size()-1]});
            ans = min(mi, ans);               
        }
        return ans;
    }

    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size();
        int m = queries.size();
        vector<int> ans(m, -1);
        unordered_map<int, vector<int>> mp;
        for(int i = 0; i < n; i++) {
            mp[nums[i]].push_back(i);
        }
    
        for(int i = 0; i < m; i++) {
            int q = queries[i];
            if(mp.find(nums[q]) != mp.end() && mp[nums[q]].size() >=2) {
                ans[i] = findMinDis(mp[nums[q]], q, n);
            }
        }
        return ans;
    }
};
['C++']