Hash map + Binary Search | Simple to understand 🚀🫠
Intuition and Approachlooking at the constraints its quite clear that a solution with a max of O(nlogn) complexity will pass the testcases. Anything beyond that
# Intuition and Approach
looking at the constraints its quite clear that a solution with a max of O(nlogn) complexity will pass the testcases. Anything beyond that will give a TLE. So in order to achieve that complexity we definitely need to use some data structure which can help us locate all the positions of an element so that we can easily calculate the distances.
And in such scenarios its usually a hashmap solution so we use this to store all the positions of an element.
The map will look like : map<int, vector<int>> where this vector will store the positions.
Now for each query we need to first locate the index value the query is referring to in our map. Now for locating the idx, either we can do a linear search or preferably a binary search which will save us some time.
Finally after getting the location of the idx value from the vector, we need to calculate the distance between the current position of the element and the prev and next positions of the same element.
Refer to the code for this its quite straight forward.

# Complexity
- Time complexity: O(nlogn)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int find_idx(int idx, vector<int>& search_space, int k) {
        int low = 0, high = k-1;

        while(low <= high) {
            int mid = (low + high)>>1;

            if(search_space[mid] == idx) return mid;
            else if(search_space[mid] < idx) low = mid + 1;
            else high = mid - 1;
        }

        return -1;
    }
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = nums.size(), m = queries.size();
        vector<int> ans;
        
        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 num = nums[queries[i]];
            
            if(mp[num].size() == 1) ans.push_back(-1);
            else {
                vector<int>& search_space = mp[num];
                
                int k = search_space.size();
                int idx = find_idx(queries[i], search_space, k);
                    
                int prevIdx = idx-1 < 0 ? k-1 : idx-1;
                int nextIdx = idx+1 >= k? 0 : idx+1;
                
                int nextDist, prevDist;
                nextDist = (nextIdx < idx)? n - search_space[idx] + search_space[nextIdx] : search_space[nextIdx] - search_space[idx];
                prevDist = (prevIdx > idx)? search_space[idx] + n - search_space[prevIdx] : search_space[idx] - search_space[prevIdx];
                
                ans.push_back(min(nextDist, prevDist));
            }
        }
        
        return ans;
    }
};
```

['C++']