Map based Implementation
ApproachStore all the occurances of all numbers of nums in a map(or hashmap).
for all queries check the max difference between its previous and next occurance.C
# Approach
Store all the occurances of all numbers of nums in a map(or hashmap).
for all queries check the max difference between its previous and next occurance.

# Complexity
- Time complexity:
O(nlogn)

- Space complexity:
O(n)

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        map<int,vector<int>> m;
        int n = nums.size();
        for(int i=0;i<n;i++){
            m[nums[i]].push_back(i);
        }
        vector<int> res;
        for(auto q:queries){
            int k = nums[q];
            if(m.find(k)==m.end()){
                res.push_back(-1);
            }
            else{
                vector<int> &v = m[k];
                int idx = lower_bound(v.begin(),v.end(),q)-v.begin();
                if(v.size()==1){
                    res.push_back(-1);
                }
                else{
                    int p;
                    cout<<idx<<v.size()<<endl;
                    if(idx==0){
                         p = min(v[1]-v[0],n-v[v.size()-1]+v[0]);
                    }
                    else if(idx == v.size()-1){
                        p = min(v[v.size()-1]-v[v.size()-2],n-v[v.size()-1]+v[0]);
                    }
                    else{
                        p = min(v[idx]-v[idx-1],v[idx+1]-v[idx]);
                    }
                    res.push_back(p);
                }
            }
        }
        return res;
    }
};
```
['Hash Table', 'Binary Search', 'C++']