Simple Hash Map || Binary Search||200ms runtime||beat100% ||
IntuitionAs we need index of least distance here we need hash map to store index with constant search timeApproachWe will create a unorederd map to store value
# Intuition
As we need index of least distance here we need hash map to store index with constant search time 
# Approach #
We will create a unorederd map to store value in key and indices in value vector eg-{unordered_map<int,vector<int>>m}
and for each queries we will find index of queries in map vector and contain left and right indices ans we calculate distance from left and right indices and store it in ans vector . We map vector contain only one vector the we no there is no duplicate of it so we store-1
{
for  Better Understanding see the provided code
}

# Complexity
- Time complexity:
time comlexity will be O(n)+O(nlog(n)) =O(nlog(n))

- Space complexity:
- O(n) because we are storing every index in hash map
# Code
```cpp []


class Solution {
public:
    int ch(vector<int>& arr, int size, int target) {
        int left = 0, right = size - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] == target) {
                return mid;
            }
            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {
        int n = queries.size();
        vector<int> ans(n, -1);
        unordered_map<int, vector<int>> m;
        int m1 = nums.size();

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

      
        for (int i = 0; i < n; i++) {
            if (m[nums[queries[i]]].size() <= 1) {
                continue;
            }
            int mn = INT_MAX;
            int index = ch(m[nums[queries[i]]], m[nums[queries[i]]].size(), queries[i]);

            if (index == 0) {
                int v = m[nums[queries[i]]].back();
                int v2 = m[nums[queries[i]]][1];
                mn = min(abs(v2 - queries[i]),
                         min(abs(m1 - v + queries[i]), abs(m1 - queries[i] + v)));
            } else if (index == m[nums[queries[i]]].size() - 1) {
                int v2 = m[nums[queries[i]]].front();
                int v = m[nums[queries[i]]][index - 1];
                mn = min(abs(v - queries[i]),
                         min(abs(m1 - v2 + queries[i]), abs(m1 - queries[i] + v2)));
            } else {
                int v = m[nums[queries[i]]][index + 1];
                int v2 = m[nums[queries[i]]][index - 1];
               mn = min(abs(v - queries[i]),abs(v2-queries[i]));
            }
            ans[i] = mn;
        }
        return ans;
    }
};

```
['Hash Table', 'Binary Search', 'C++']