Binary Search | Map | Easy Logical Explanation | Code with comments | O(nlogn)
LogicIf we store the indexes of elements by traversing the array, can we say that indexes of each element would be in sorted manner?can we get the min distance
# Logic
If we store the indexes of elements by traversing the array, can we say that indexes of each element would be in sorted manner?

can we get the min distance from there by comparing adj indexes present for each array?

so array is sorted can we think about binary search to get the index of current occurence in index array bcs linear search would be costly, so get the index by binary search and compare adj index to get min distance for each query.

# Complexity
- Time complexity: O(q*log(n)) 
    q is size of queries and n is the size of nums
    in worst case we have to do binary search for whole nums array, when all elements present in arrays are equal
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)+O(q)
    n for storing indexes into map, for each element there would be one entry
q for ans array, storing min distance for each query
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        //map to store nums elements and it's indexes in sorted order
        unordered_map<int,vector<int>> mp;
        for(int i=0;i<nums.size();i++){
            mp[nums[i]].push_back(i);
        }

        //array to store querie's result
        vector<int> ans;
        for(auto i:queries){
            int n = nums[i];
            // single occurence of nums[i], can't find distance so push -1
            if(mp[n].size()==1){
                ans.push_back(-1);
                continue;
            }

            // find the index of nums[i] in index array of nums[i] from map using binary search
            int ind = lower_bound(mp[n].begin(),mp[n].end(),i)-mp[n].begin();

            // find prev and next to get the min distance
            int prev = (ind + mp[n].size() - 1)%mp[n].size();
            int next = (ind+1)%mp[n].size();

            // prev index should be less than ind, if it's not means it's pointing to last occurences of nums[i]
            // if prev is bigger than ind then get the actual last index of nums[i] if copy of nums inserted before the original copy
            if(prev>ind){
                prev = mp[n][prev]-nums.size();
            } else {
                prev = mp[n][prev];
            }

            // same thing we do to get the next index of nums[i]
            // if next is smaller then it's pointing to first occurence of nums[i], get the actual last index if nums copy inserted after origina nums array
            if(next<ind){
                next = mp[n][next]+nums.size();
            } else {
                next = mp[n][next];
            }
            
            //get the min distance by comparing adj indexes and store in ans array
            ans.push_back(min(i-prev, abs(next-i)));
        }
        return ans;
    }
};
```
['C++']