✅ C++ sol. using MAP and BINARY SEARCH
Code


# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size();
        vector<int>ans;

        unordered_map<int , vector<int>>m;

        for(int i=0 ; i<n ; i++){
            m[nums[i]].push_back(i);
        }

        for(auto q : queries){
            int dis = n ;
            auto it = upper_bound(m[nums[q]].begin() , m[nums[q]].end() , q);
            auto it2 = lower_bound(m[nums[q]].begin() , m[nums[q]].end() , q);
            if(it != m[nums[q]].end()){
                int in = *it ;
                dis = min(dis , in - q);
                dis = min(dis , n - m[nums[q]].back() + q);
            }
            
            if(it2 != m[nums[q]].begin()){
                it2 = prev(it2);
                int in = *it2;
                dis = min(dis , q - in);
                dis = min(dis , n - q + m[nums[q]][0]);
            }

            if(dis >= n){
                ans.push_back(-1);
            }
            else{
                ans.push_back(dis);
            }
        }

        return ans ;
    }
};
```
['Array', 'Hash Table', 'Binary Search', 'C++']