Binary search and math
IntuitionWe need to check only the adjacent two elements ie left and right elements when arranged in circular manner.ApproachUse a map to store indices of all o
# Intuition
We need to check only the adjacent two elements ie left and right elements when arranged in circular manner.

# Approach
Use a map to store indices of all occurences of each distinct element. Since we're traversing in left to right direction, the index vectors are by already in sorted order.
If there is single occurence of $$nums[queries[i]]$$, push -1 to result. **(Edge case)** 
Else, locate queries[i] in $$mp[nums[queries[i]]]$$ using binary search.
If it is the first element, use $$lastElement - n$$ as left neighbour.
If it is the last element, use $$firstElement + n$$ as right neighbour.

# Complexity
- Time complexity:
$$O(n*log(n))$$

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

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        vector<int> res;
        int n=nums.size();
        unordered_map<int,vector<int>> mp;
        for(int i=0;i<nums.size();i++){
            mp[nums[i]].push_back(i);
        }
        for(auto q:queries){
            if(mp[nums[q]].size()==1) {res.push_back(-1);continue;}
            int prv=lower_bound(mp[nums[q]].begin(),mp[nums[q]].end(),q)-mp[nums[q]].begin();
            int ans;
            if(prv==0){
                ans=abs(mp[nums[q]][prv]-mp[nums[q]][prv+1]);
                ans=min(ans,abs(mp[nums[q]][prv]-(mp[nums[q]].back()-n)));
            } 
            else if(prv==mp[nums[q]].size()-1) {
                ans=abs(mp[nums[q]][prv]-mp[nums[q]][prv-1]);
                ans=min(ans,abs(mp[nums[q]][prv]-(mp[nums[q]][0]+n)));
            }
            else{
                ans=abs(mp[nums[q]][prv]-mp[nums[q]][prv+1]);
                ans=min(ans,abs(mp[nums[q]][prv]-mp[nums[q]][prv-1]));
            }
            res.push_back(ans);
        }
        return res;
    }
};
```
['C++']